Caesars Cipher and join()

Tell us what’s happening:
Hiya, I’m having a heck of a time trying to join the elements of the array of the code points for the challenge. I created a for loop to sift through the numbers, and used result.push(str.charCodeAt(i) to add to the array. I know the elements need to be joined to create a string in order for String.fromCharCode() to work. However, I cannot use result.join() anywhere in the loop in order to combine the elements of the array together to create a string. Looking for hints w/o code is tricky, so if anyone can help me understand why without revealing any code that would be super. Thanks!

Your code so far

function rot13(str) { // LBH QVQ VG!
var result=[];
  for(i=0;i<str.length;i++){
    if(str.charCodeAt(i)<78){
     result.push(str.charCodeAt(i) + 13);
    } 
    if(str.charCodeAt(i)>78){
     result.push(str.charCodeAt(i)- 13);  
    }
  }
return result.join();}

rot13("AERR PBQR PNZC");```
**Your browser information:**

Your Browser User Agent is: ```Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36```.

**Link to the challenge:**
https://www.freecodecamp.org/challenges/caesars-cipher

Your problem with join is simply that the default string to join with is ',', when you want to join together with no gaps (pass the empty string like this: .join('')).

There are also some other issues with your code. In particular, you’ll need to use String.fromCharCode() as well as str.charCodeAt(). After that, there are also a couple of other small bugs to work out.

1 Like

Thanks for the help! It seems that when I use return.join() it returns the following:

78,82,69,69,45,57,79,68,69,45,67,77,80

…which is understandable, since I have not used String.fromCharCode() to convert to alphanumeric. I’m attempting to join the elements and then return them using String.charCodeAt(), however this will not work as the only thing that displays when I return that is a red dot that displays. Any thoughts?

Consider the logic you need to implement (pseudocode):

if the char code of the current char in the range 65..77:
  take the char code of the current char
  add 13
  convert the resulting char code back into a char and use that
else if it's in the range 78..90:
  do the same thing, but subtracting 13 instead
else
  just use the char as-is (it's not an uppercase letter, shouldn't be shifted)

then join the chars back up

Is this what your code’s actually doing? The red dots are probably control characters, most of which are very low codepoints (0..31).

Thanks again, Lionel. Yes, what you described is actually what my thought process was. I did get the code to work after banging my head against the wall for a good 8 hours, albeit in a long-winded manner. The missing piece was the result.push(String.fromCharCode(str.charCodeAt(i)). I was clinging stubbornly to the idea I could join the the code points in an array and then use Sting.fromCharCode in the final return. Here’s what I came up with:

var result=[];
  for(i=0;i<str.length;i++){
    if(str.charCodeAt(i)>=65 && str.charCodeAt(i)<=90){
      if(str.charCodeAt(i)<78){
       result.push(String.fromCharCode(str.charCodeAt(i) + 13));
    } 
      if(str.charCodeAt(i)>=78){
       result.push(String.fromCharCode(str.charCodeAt(i) - 13));
      }
    }else result.push(str[i]);
    
}return result.join('');
}

Being a young sprout, I’m not sure exactly what does or does not constitute ugly code, but I’m going to go with my gut on this and say that definitely is.

Thank you again for your help!