Caesars Cipher: bug with characters

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.

Link to the challenge:

Also, most letters are where they should be. It is supposed to shift 13 characters, but I only have a 12 because it made most of them work.

update: figured it out, but now I have a different problem

gods job in figuring it out yourself!

if you need help with the different problem, you will also need to post the new code

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.

what is str[str.length]?

str[str.length] comes up as an empty string.

don’t you see any issue there?
what’s the index of the last character in your string?

also, now that I see it better, that is wrong syntax… i => str.length is wrong syntax - not just bad logic

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.

you probably missed this part

yes I did miss that part lol, ill try it

that worked, thanks!