Caesars Cipher - Loop only running one letter

Tell us what’s happening:
The code only changes the first letter and then seems to jump out of the loop.
I don’t know where i’m going wrong

Your code so far

function rot13(str) { // LBH QVQ VG!
  str = str.split('');
  var x;
  for(var i =0;i<str.length;i++)
      if(str[i].charCodeAt(i) == 32)
      if(str[i].charCodeAt(i)>=65 && str[i].charCodeAt(i)<=90)
          x =  str[i].charCodeAt(i);
          x = x - 13;
          str[i] = String.fromCharCode(x);
  str = str.join('');
  return str;

// Change the inputs below to test
rot13("SERR PBQR PNZC");

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36.

Link to the challenge:

You have the following written in a couple places in your code. Why are you using charCodeAt(i)?


To help you understand what is going on here, let me walk you through the 2nd and 3rd iterations of your for loop.

2nd iteration:
i = 1
str[1] = ‘E’
str[1].charCodeAt(1) = NaN

3rd iteration:
str[2] = ‘R’
str[2].charCodeAt(2) = NaN

Why NaN? Because there is only one character in str[1] or str[2], so when you write charCodeAt(1) or charCodeAt(2), you are trying to reference characters beyond one character, and when the index is out of range, JS returns NaN as the value.

Hint: Read about the index Parameter of charCodeAt and you should see what to do.