Tell us what’s happening:
Can’t figure out how to get the array or string from numbers back to letters. Ideas?
Your code so far
function rot13(str) {
var nArr = [];
var number;
var letter;
for (i=0; i<str.length; i++){
if (65 <= str.charCodeAt(i) && str.charCodeAt(i) <= 90){ //if it's between A-Z convert it
number = str.charCodeAt(i)+13; //convert + 13
if (number > 90) { //if the number is above 90 (Z) minus 26 to bring it down to range 65-90 (A-Z)
number = number - 26;
}
letter = String.fromCharCode(number); //convert to letter
nArr.push(letter); //push letters to array
}
else nArr.push(str[i]);
}
nArr = nArr.join('');
return nArr;
}
// Change the inputs below to test
rot13("SERR CVMMN!");
Your browser information:
Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36.
That’s the last part, right? What about another if statement? If the number is over 77 then do something special to it? If statements are so sloppy tho, must be a cleaner way.
There’s nothing inherently wrong with if statements. They’re super useful. If you find yourself with an elaborate system of if/else statements, then you might want to restructure your logic.
I had an i in the push method so it was pushing the position of the space (4) and the ! (10). Current code works, I just need it to push the spaces, ! and ? through.