My code seems to have a bug where when I put certain letters such as “E” it bugs out and makes it a letter behind where it should be. Any suggestions? (I am not 100% done with the code, so I haven’t combined the characters.)
Your code so far
function rot13(str) { // LBH QVQ VG!
str = str.split('');
let n;
for(let i = 0; i => str.length; i++) {
n = str[i].charCodeAt(0);
n = n + 12;
if (n > 90) {
n = (n - 90) + 65;
}
console.log(String.fromCharCode(n))
str[i] = String.fromCharCode(n);
}
return str;
}
// Change the inputs below to test
rot13("SERR PBQR PNZC");
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1.2 Safari/605.1.15.
function rot13(str) { // LBH QVQ VG!
str = str.split('');
let n;
for(let i = 0; i => str.length; i++) {
n = str[i].charCodeAt(0);
if (n === 32) {
n = n - 13;
}
n = n + 13;
if (n > 90) {
n = (n - 90) + 64;
}
str[i] = String.fromCharCode(n);
console.log(str[i])
}
str = str.join('');
return str;
}
// Change the inputs below to test
rot13("SERR PBQR PNZC");
I figured out that problem, but now it says that undefined is not an object (evaluating ‘str[i].charCodeAt’), even though when I use console.log it works perfectly.
yeah, that’s probably a problem. But if I revise the program so the for loop is as follows:
for(let i = 0; i => str.length - 1; i++) {
n = str[i].charCodeAt(0);
if (n === 32) {
n = n - 13;
}
n = n + 13;
if (n > 90) {
n = (n - 90) + 64;
}
str[i] = String.fromCharCode(n);
}
the same error message appears when I run the code.