Caesars Cipher weird console log result

Tell us what’s happening:
Hey everyone, I am struggling with this problem currently. So what is happening is thet in the console, I get a red dot. What could be wrong? This is happening after I first tried to use return strArr.fromCharCode().

Your code so far

function rot13(str) { // LBH QVQ VG!
  var strArr=str.split("");
  var newArr = [];
  //return strArr[1].charCodeAt();
  for(i=0;i<strArr.length;i++) {
    strArr[i]=strArr[i].charCodeAt();
      if (strArr[i]>=65 && strArr[i]<91) {
        if(strArr[i]<78) {
          strArr[i]+=13;
        }
        if(strArr[i]>=78) {
          strArr[i]-=13;
        }
      }
   
   
    
    
  } 

  var text = String.fromCharCode(strArr);
  newArr.push(text);
  return newArr.join("");
  

  
  
}

// Change the inputs below to test
rot13("SER 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/64.0.3282.186 Safari/537.36.

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

var text = String.fromCharCode(strArr);

The way you use String.fromCharCode() is as follows

String.fromCharCode(65, 66, 67);

The way you don’t use String.fromCharCode() is as follows

String.fromCharCode([65, 66, 67])

(The above is the same as String.fromCharCode(NaN), by the way)

You will need to spread out your array into arguments to fix the issue.

var text = String.fromCharCode(...strArr);

String.fromCharCode() documentation

I gave it some more thought and noticed that strArr is made of the numbers divided by just commas. So I tried introducing a variable called split that makes it into a string that divides them using a comma and a space like in the fromCharCode link::

var split = strArr.join().split(",").join(", ");

But it doesn’t seem to change anything.

No it isn’t :slightly_smiling_face:. It’s an array. However, when you convert an array to a string (maybe you’re trying to show it somewhere for debugging and it’s done that for you?), it’ll, by default, separate the elements with commas. Your code is correct, though, that’s just the string version of the array.

I suggest you read my earlier reply to understand what the actual issue is. :slightly_smiling_face:

I was writing the second message as you replied.:smile: Also, for some reason it doesn’t change the letter E into R. E’s code is 69 and the algorithm should add 13 to it to become 82®. But instead in the strArr, it still shows up as 69 at the end. The other letters seem to be fine though. I’m trying to see at what step this gets omitted but am at a loss.:frowning:

Sure thing. Sorry if formatting is not too good. Still getting used to it.

function rot13(str) { // LBH QVQ VG!
  var strArr=str.split("");
  var split = "";
  //return strArr[1].charCodeAt();
  for(i=0;i<strArr.length;i++) {
    strArr[i]=strArr[i].charCodeAt();
      if (strArr[i]>=65 && strArr[i]<91) {
        if(strArr[i]<78) {
          strArr[i]+=13;
        }
        if(strArr[i]>=78) {
          strArr[i]-=13;
        }
      }
   
   
    
    
  } 
split = strArr.join().split(",").join(", ");
 
return String.fromCharCode(split);
  
  

  
  
}

// Change the inputs below to test
rot13("ESERR PBQR PNZC???");

Please read my post. The below line

split = strArr.join().split(",").join(", ");

does not fix the issue. Instead, please use the spread operator as I said. The function returns a single character because it’s trying to cast a weird string-representation of an array as a number. It can’t do that.

Leave it as an array, and instead just use the spread operator. :slightly_smiling_face:

Afterwards, it’ll at least return a sequence of characters. Your code seems to have some issues, though, with the ROT13 aspect – and that’s a separate issue which needs to be debugged.

Good luck!

Oh I see. I thought you used “…” to stand for something that I need to figure out myself:smile:. Thank you.

2 Likes

Maybe my understanding of what ROT13 implies is off. So, for the each out of the first 13 letters, My code should replace the letter with the one that is after 13 places in the alphabet and for the last 13 letters, my code should replace each with the letter that is 13 places before, right?

Ok so I made a small change that I thought would be useless and it actually solved everything.

<p> function rot13(str) { // LBH QVQ VG!
  var strArr=str.split("");
  //return strArr;
  //return strArr[1].charCodeAt();
  for(i=0;i<strArr.length;i++) {
    strArr[i]=strArr[i].charCodeAt();
      if (strArr[i]>=65 && strArr[i]<91) {
        if(strArr[i]<78) {
          strArr[i]+=13;
        } else {
          if(strArr[i]>=78) {
          strArr[i]-=13;
        }
        }
        
      }
   
   
    
    
  } 
//split = strArr.join().split(",").join(", ");
 
return String.fromCharCode(...strArr);
  
  

  
  
}

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

So within the first if statement, there were to other if statements one after the other. I put the last if statement within the else of the other one and it solved everything. Can someone please explain why? Did the for statement go to the next number in the array if the previous number did not meet the condition of the first if or why did some numbers stay unchanged?

The reason why what you did fixed it is because first it would check if it’s less than 78.

Let’s say your number is 75. Then it changes the actual value to 88.

The next if statement checks if it is more than 78. 88 is more than 78, so it changes it back to 75, and that’s why it wasn’t working before.

By making sure only one of them ever runs, you can never go forwards and then backwards in one go. :slightly_smiling_face:

1 Like

I see now. I’ve been trying to figure this out for the last few hours. Thank you everyone!