Test Cases pass in Java but fail in JavaScript

Hello everyone, I noticed something weird when doing the following challenge:

I wrote the solution for the challenge in javascript and it got the manual test case to work correctly, however when I published it it said wrong answer: test cases failed.

the problem is that the failed test cases viewer is not supported with javascript.

I translated the code into pure java, and it worked perfectly:

JS:

importPackage(java.io);
importPackage(java.lang);
 
let reader = new BufferedReader( new InputStreamReader(System['in']) );

let t = parseInt(reader.readLine());

for (let i = 0; i<t; i++){
    let s = String(reader.readLine());
    let f = true;
    for(let j = 0; j<s.length; j+=2){
        if (s.substr(j, j+2)=="AA" || s.substr(j, j+2)=="BB"){
            f = false;
        }
    }
    print(f ? "yes" : "no");
}

Java:

class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner sc = new Scanner(System.in); 
		int t = sc.nextInt(); 
		for (int i = 0; i<t; i++){
		    String s = sc.next();
		    //System.out.println(s);
		    boolean flag = true;
		    for (int j = 0; j<s.length(); j+=2){
		        if (s.substring(j, j+2).equals("AA")||s.substring(j, j+2).equals("BB")){
		            flag = false;
		        }
		    }
		    System.out.println(flag ? "yes" : "no");
		}
	}
}

Can anyone tell me why this problem occurs - am I missing something in my code or are there compatibility problems with javascript and the codechef servers? thanks in advance for the replies

1 Like

string.substr(start , length) in js, so replacing s.substr(j,j+2) with s.substr(j, 2) should work

okay i see it now, confused the substr with the js substring method, thanks for the clarification!