Stuck on Caesars Cipher Challenge

Hello & happy Thursday fellow coders!

I feel like i am on the right path with the logic here, but i can not execute the final steps of what I am trying to do - convert the ASCII code back to a string.

Code So Far:

function rot13(str) {
let charCodeArr = ;
for (let i = 0; i < str.length; i++){
let code = str.charCodeAt(i);
if (str.charCodeAt(i) >= 65 && str.charCodeAt(i) <= 77){
charCodeArr.push(code + 13);
}
if (str.charCodeAt(i) > 77 && str.charCodeAt(i) <= 90){
charCodeArr.push(code - 13) }
if (str.charCodeAt(i) < 65){
charCodeArr.push(code);
}
}
let decoded = charCodeArr.toString();

return String.fromCharCode(decoded);
}

I am thinking I am not using String.fromCharCode() method correctly but I am stuck in trying to figure out how else to convert the stored ASCII values to create the decoded string. Hope I am making sense…

Any insight and advice would be greatly appreciated!

Thank you!

Julie

You use String.fromCharCode() with a string of numbers (eg. “70,82,69,69,32”) so it doesn’t work. You may want to:

(1) iterate through charCodeArr and convert each charCode into character then join the array, or
(2) convert the charCodes to characters before pushing them into charCodeArr , or
(3) use destructuring syntax if you’re familiar with it

String.fromCharCode(...charCodeArr )

Also, your conditions don’t mention the case when str.charCodeAt(i) > 90 so you may miss some characters in the result.

Finally, when posting in the forum, you should wrap your code in three backticks ( ``` ) so it’s easier to read :

function rot13(str) {
  let charCodeArr =[] ;
  for (let i = 0; i < str.length; i++){
1 Like

Aha! With a couple quick fixes of adding the condition of > 90 and using the destructuring syntax in the return statement did the trick.
Thank you so much for your guidance on this challenge.
Also, appreciate the reminder to wrap code! :smiley:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.