Caesars Cipher- Code review

Caesars Cipher- Code review
0.0 0

#1

Tell us what’s happening:
Hi there!

As a continuation to learn, I hope you can assist me in debugging my code. I’ve read some notes elsewhere but am trying not to spoil solutions. I’m sure this isn’t the most effective solution, but it was what I started with and if not right route, point how I can make it better!

Thank you :slight_smile:
PSEUDO:

  1. Define variables.
  2. Recognize alphabet ASCII is between 65->90, need to pass special characters (value outside this range)
  3. Create a for loop based on the function length
  4. Nest if statement. The ifstatement uses str.CharCodeAt to add 13 to each value and assign it to an array. Place an else statement to return the original value if outside 65-90 value
  5. Nest another if statement to subtract 26 if larger than 90
  6. Use join to convert the array to str
  7. Convert the numbers back to a string
  8. Return final str

Issues:
a) The characters are still recognized as between 65-90 and have 13 added to them
b) The Str.fromCharCode isnt converting it.

Your code so far

function rot13(str) { // LBH QVQ VG!
  var arr =[];
  var num = 0;
  var newstr="";
  var finalstr = "";

 for (i = 0;i<str.length;i++){
    if (str.charCodeAt(i)>=65 && str.charCodeAt(i)<=90){

    arr[i] = str.charCodeAt(i)+13;
      if(arr[i]>90){
        arr[i]=arr[i]-26;
      }
}
    else{
      arr[i]=str.charCodeAt(i);
    }
 }

 
newstr=arr.join();

 String.fromCharCode(newstr);
  
 return newstr;
}

// Change the inputs below to test
rot13("SERR CVMMN!");

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0.

Link to the challenge:


#2

You are checking if the ASCII code is greater or equal to 65 OR is less than or equal to 90.

If the ASCII code for a given character is 100, then your if statement is true because 100 >= 65. Also, your if statement is true is the number is 2, because 2 <= 90. Basically, any number will cause your if statement to be true, so the code in the else block is never executed.

The above is only one problem, but it will get you a little farther along.


#3

Hi Randy,

Thank you for the quick reply and solving one issue. That was very careless and it’s so frustrating when you just even forget the basics.

Will improve moving forward and try to complete