I can get each line to work separately; but I can’t get the my returnStr to accept the new values: returnStr is undefined?
function rot13(str) { // LBH QVQ VG!
str = str.split('');
var rot13 = 13;
var returnStr =[];
for (var i = 0; i < str.length; i++){
if (str[i].charCodeAt(0) != " "){
var tempAsciiVal = str[i].charCodeAt(0)-64;
var offset = tempAsciiVal - rot13;
var tempChar = "";
//just replace it.
if (offset > 0){
tempChar = str[i].charCodeAt(0) - rot13;
returnStr[i].push(String.fromCharCode(tempChar));
} //deal with : gone negative: pack it ontop.
else {
tempChar = String.fromCharCode(90 + offset);
returnStr[i].push(tempChar);
}
} else{
returnStr[i] = str[i];
}
}
return returnStr;
}
// Change the inputs below to test
rot13("ERR PBQR PNZC");
A common modern use is the ROT13 cipher, where the values of the letters are shifted by 13 places. Thus ‘A’ ↔ ‘N’, ‘B’ ↔ ‘O’ and so on.