Caesar's Cipher help

Caesar's Cipher help
0.0 0

#1

I don’t understand why the String.fromCharCode() method won’t convert the character codes back to their respective characters (see commented out text toward bottom). I’m so close, yet so far.

function rot13(str) { 
  
 var newArray = [];
  
 for (i = 0; i<str.length; i++) {
   
   if (str.charCodeAt(i) < 65) { 
     var nonLetter = str.charCodeAt(i);
     newArray.push(nonLetter);
     
   } else if((str.charCodeAt(i) + 13) > 90) {
       var newLetter = str.charCodeAt(i) - 13;
       newArray.push(newLetter);
     
   } else {
       var codeLetter = str.charCodeAt(i) + 13;
       newArray.push(codeLetter);
   }

 }
  
 var arrToString = newArray.toString();
 return arrToString; //outputs 70,82,69,69,32,67,79,68,69,32,67,65,77,80
  
 //var finalCode = String.fromCharCode(arrToString);
 //return finalCode;
// (previous return statement commented out for this is run)
// Outputs a red * but I expected FREE CODE CAMP

}     

rot13("SERR PBQR PNZC");

#2

As you seem to understand, the Array.prototype.toString() function will “Stringify” the array. If you notice, the string includes “,” as a separator. I think this is why you are confused. A string object with commas in it is NOT the same as a parameter list separated by commas (which is javascript syntax). The CharCodeAt() method requires a bunch of parameters. You probably want the rest operator, covered in the FCC Beta.


#3

Oh okay. Thank you. This was my first time trying Array.prototype.toString() so I wasn’t sure exactly how it worked. Your explanation makes sense.


#4

Sure thing. Happy to help when I can and when the requester bothers to actually formulate a question instead of just pasting code with the title “what is happening?”


#5

Also, dude, LIVE on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects


#6

Not helpful but you should use the code button when posting code!
It’s in the toolbar, it looks like this --> </>
:slight_smile:


#7

Better yet: Markdown! (works on github and reddit, too!)


#8

I was wondering why my post didn’t look right lol. I’ll make sure i use it in the future. Thanks!


#9

I’ll give that a try


#10

Another option which is similar to using the spread operator, is to use the apply function.

var finalCode = String.fromCharCode.apply(null, newArray); // the apply function allows you to treat the array as arguments for the fromCharCode function.

Also, I’ve edited your original post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#11

Yes. Thanks. I swapped the names. The ellipsis(…) is called the rest operator when you you use it on the parameter list when defining a function, meaning “the rest of the parameters go into an array”. It is called the spread operator when you want to use an array as multiple variables, you “spread the array items out”. Stack overflow explains this really well with code examples here.