Caesars Cipher - Why isn't it working?

Caesars Cipher - Why isn't it working?
0.0 0

#1

Tell us what’s happening:

Your code so far

function rot13(str) { // LBH QVQ VG!
  var codedLetter = "";
  var uncodedLetter = "";  
  var newString = "";
  for(var i=0;i < str.length;i++){
    if(str.indexOf(i) == ""){
      uncodedLetter = " ";
    }else{
      codedLetter = str.charCodeAt(i)-13;
      uncodedLetter = String.fromCharCode(codedLetter);
      //console.log(codedLetter,uncodedLetter,newString);
    }
      newString = newString + uncodedLetter;
    console.log(newString);  
    //return newString;
  }
return newString;
}
// Change the inputs below to test
rot13("SERR PBQR PNZC");```
**Your browser information:**

Your Browser User Agent is: ```Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36```.

**Link to the challenge:**
https://www.freecodecamp.org/challenges/caesars-cipher

#2

First of all, the following line will always evaluate to false, because indexOf yields integers from -1 through the length of a string minus 1.

if(str.indexOf(i) == ""){ // always evaluates to false

so only your else block of code will execute. In the else block of code, you have the following:

codedLetter = str.charCodeAt(i)-13;
uncodedLetter = String.fromCharCode(codedLetter);

In the example string “SERR PBQR PNZC”, the letter “E” and “C” will not be letters A-Z after you subtract 13 from their ASCII values, because the ASCII values of the letters A-Z are 65-90 respectively. You need to rethink your logic for when values calculate to below 65 after subtracting 13 from the ASCII value.

Also, you need to only convert letters A-Z and not anything else. Your current code does not do that.