Caesars Cipher not working

Caesars Cipher not working
0

#1

Tell us what’s happening:

My code is not working please tell me whats wrong ?

Your code so far

function rot13(str) { // LBH QVQ VG
  var a=[];
  for(i=0;i<str.length;i++){
    if(str.charCodeAt(i) < 65 || str.charCodeAt(i) > 90)
      return a.push(str.charCodeAt[i]);
    
    else if(str.charCodeAt(i)>77)
      return a.push(String.fromCharCode(str.charCodeAt(i)-13));
    
  else 
  return a.push(String.fromCharCode(str.charCodeAt(i)+13));

  }
  return a.join("");
}

// 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/62.0.3202.94 Safari/537.36.

Link to the challenge:


#2

Not sure if you’re still working on this, but if you are, take a look at where your return statements are.


#3

As soon as a return statement is encountered, the function is exited and the value specified after the return will be returned to the calling function. The return statement will exit the function even if a for loop has not been completely iterated. Your for loop is only making through one iteration before one of the first 3 return statements executes. The last line of the function (below) will never be executed.

return a.join("");

because one of the 3 above it will have already caused the function to exit.


#4

thank you very much, i completed the code after i had posted it. I had intended to remove it but i forgot, anyways Thank you very much for going through it :smiley: i really appreciate your efforts.
Cheers have a great day.
Thanks.