Challenge- Caesars Cipher

Challenge- Caesars Cipher
0.0 0

#1

Hi FCCers,

Hope you all had a good start in 2017! I’m having difficulty with this challenge- Caesars Cipher, I can’t work out what goes wrong with my code. Can someone please help me? Thanks! :smiley:

function rot13(str) { 
 var result ="";
  for(var i =0; i<str.length; i++){
    if(str.charCodeAt(i)<65||str.charCodeAt(i)>90){
    result.push(str.charAt(i));
    } else if(str.charCodeAt(i)>77){
     
      result.push(str.fromCharCode(str.charCodeAt(i)-13));
    }else {
       result.push(str.fromCharCode(str.charCodeAt(i)+13));
    }
   
  }
  return result;
}

#2

I have a feeling you dont debug (step through) your code … so before i give you the mistakes i ask that you try using this link i supplied below … paste your code in it and set it for javascript ESB then you can step through each line and see where problems are … it will be a great help to you in the future … i use this all the time

still have problems … i have to go out now be back shortly and will list what i found

http://www.pythontutor.com/


#3

You have incorrect syntax for fromCharCode() and you are trying to .push() to string (result is a string).


#5

Hey thanks for the link. I’m still playing with the tool, haven’t really figured out how it works.


#6

ughhh thanks for the advice! I’ve corrected the code according to your feedback. and it passed! thanks a lot!

function rot13(str) { 
 var result =[];
  for(var i =0; i&lt;str.length; i++){
    if(str.charCodeAt(i)&lt;65||str.charCodeAt(i)&gt;90){
    result.push(str.charAt(i));
    } else if(str.charCodeAt(i)&gt;77){
     
      result.push(String.fromCharCode(str.charCodeAt(i)-13));
    }else {
       result.push(String.fromCharCode(str.charCodeAt(i)+13));
    }
   
  }
  return result.join("");
}

#7

glad to see you got it sorted … dont give up on pythontutor as its great for stepping through code to see what is going on and for finding errors