Caesars Cipher is hacked!

Why is this code not passing the test ?..but it works tho.

var newString = “”;

function rot13(str) { // LBH QVQ VG!
for (i = 0; i< str.length ; i++){
if ( str.charCodeAt(i)> 77) {
newString += String.fromCharCode(str.charCodeAt(i)-13);
} else if (str.charCodeAt(i) < 65){
newString += str[i];
}else if ( str.charCodeAt(i)<= 77){
newString += String.fromCharCode(str.charCodeAt(i)+13);
}

}
return newString;

}

// Change the inputs below to test
rot13(“SERR PBQR PNZC”);

put var newString = ""; inside the function .The reason being that FCC run multiple test cases in on the code, using a global var like this persists the last value and hence the test cases fail.

More generally, if you were writing code like this in production, you’d definitely want to be initializing that variable within the function, otherwise the function wouldn’t have the intended effect if run more than once. That’s even without considering pollution of the global namespace (it’s highly likely you’d want to use a variable name as generic as newString elsewhere in your code, which could cause all manner of problems).

Wow, thanks guys …i have been enlightened.