Caesar's Cipher decoding some characters

function rot13(str) { // LBH QVQ VG!
  var arr = [];
  
  for(var i = 0; i < str.length; i++)
  {
    if(str.charAt(i) != ' ')
    {
      arr[i] = String.fromCharCode(str.charCodeAt(i) - 13);
    }
    else
    {
      arr[i] = ' ';
    }
  }
  
  str = arr.join('');
  
  return str;
}

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

This results in “F8EE C5DE CAM6”.

I’m going to take a wild guess and say that it’s supposed to print FREE CODE CAMP. Why it wouldn’t decipher the R, O, or P is beyond me. Any ideas?

Insert the following line of code after your line #8 and you will see that you are using String.fromCharCode on values which are below 65. Remember the uppercase letters are from 65 thru 90.

console.log(str.charCodeAt(i) - 13);

You need to modify your code to handle when the value you create from line #8 is below 65.

I’m not sure what you mean. Where would I put that code and why would I do so?

Letters range from 65 to 90, but your code can range from 0 to infinity, which means you should create a rule so that the charCodeAt is always between 65 and 90.

Take a look at the ascii table if you need a refresher on what characters the numbers represent. You are converting from dec, decimal column, to the character.

Oh I understand, I need to wrap around.