I am having a hard time getting my code to pass. it seems like everything is coded right and it says what it is supposed to say in the terminal but when I run the code freecodecamp says it’s wrong. I’m confused.
function rot13(str) {
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
let newStr = ' ';
for(let i = 0; i < str.length; i++){
let index = alphabet.indexOf(str[i]);
if(index == -1){
newStr += str[i];
} else {
let newIndex = (index + 13) % 26;
newStr += alphabet[newIndex];
}
}
return newStr;
}
console.log(rot13("SERR PBQR PNZC"));
console.log(rot13("SERR CVMMN!"));
console.log(rot13("SERR YBIR?"));
console.log(rot13("GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT."));
Right now your code say “start with a single space and add the new letters”. You have the right logic for finding the new letters, but starting with that extra space is causing a problem.
No, you can’t put it there. Then newStr would only live inside of the loop and couldn’t collect information from all loop iterations.