Caesar's Cipher help

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");

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.

1 Like

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.

1 Like

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?”

1 Like

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

1 Like

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

1 Like

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

1 Like

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

I’ll give that a try

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.