Tell us what’s happening:
Hello there, I tried solving this project with the code below but I’m stuck as it is returning “SRRR POQR PZC”. I don’t know what’s wrong with this code. Please help me out. Thanks!
Your code so far
function rot13(str) { // LBH QVQ VG!
var newStr = str.split('');
var alphabets = String.fromCharCode(...Array(91).keys()).slice(65);
var aArr = alphabets.split('');
for(var i = 0; i < newStr.length - 1; i++) {
if(aArr.includes(newStr[i])) {
if(((aArr.indexOf(newStr[i])) + 13) > 26) {
var index = aArr.indexOf(newStr[i]) - 13;
newStr[i] = aArr[index];
}
newStr[i] = aArr[aArr.indexOf(newStr[i]) + 13];
} else newStr[i] = newStr[i];
}
console.log(newStr);
console.log(aArr.indexOf(newStr[0]));
console.log(newStr.join(''));
return newStr.join('');
}
// Change the inputs below to test
rot13("SERR PBQR PZC");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36
.
Challenge: Caesars Cipher
Link to the challenge: