Caesars Cipher weird console log result

Caesars Cipher weird console log result
0.0 0

#1

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:


#2

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


#3

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.


#4

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:


#5

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:


#6

Can you post a revised version of your code if you have made any changes since your original post?


#7

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

#8

I’ve edited your 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.


#9

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!


#10

Right before the line above executes, strArr looks like:

[ 69, 70, 69, 69, 69, 32, 67, 66, 68, 69, 32, 67, 65, 77, 67, 63, 63, 63 ]

The line (at the top) then creates the following string and assigns it to the split variable.

"69, 70, 69, 69, 69, 32, 67, 66, 68, 69, 32, 67, 65, 77, 67, 63, 63, 63"

The String.fromCharCode() function accepts one or more values as arguments and will create a string with the applicable characters. The problem is, since split is a single string value, the fromCharCode function attempts to create a character out of something that is a not a number. Instead it returns a default value of ‘\u0000’.

Instead of creating the split variable as you have done, there are a couple of options to convert strArr to the correct string. One way, would be to iterate through strArr and build a new string by using the fromCharCode function on each element to concatenate the converted characters back together. Another way involves using the apply function with the fromCharCode function, which would allow you to convert the array’s elements into argument values to pass back to the fromCharCode function. A third way is suggested by @joker314 which would be similar to using the apply function I have suggested.


#11

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


#12

You also still have some logic issues to work out which creates the strArr array of codes.

UPDATE: If the code is less than or equal to 77 you will need to add 13. Otherwise, subtract 13.


#13

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?


#14

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?


#15

The extra if is not necessary, because you already know that strArr[i] is not less than 78, because if it would have been, the if block of code would have executed strArr[i] += 13;. So, you can just use:

else {
  strArr[i] -= 13;
}

#16

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:


#17

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