Caesars Cipher Problem at charcodeat function

Tell us what’s happening:
Describe your issue in detail here.
Assalmulaikum Sir in my code the problem I am facing is that when I console the char code value using function charcodeat it gives me the wrong answer only when I am performing this in array plz check my code especially line 3 and line 9 (when i is 4)

  **Your code so far**

function rot13(str) {
let a="P";
let pattern=/[A-Z]/ig;
let result=str.match(pattern);
for (let i=0;i<result.length;i++)
console.log(i+" "+str.charCodeAt(i));
return str;

rot13("SERR PBQR PNZC");

  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36

Challenge: Caesars Cipher

Link to the challenge:

Hey your issue comes from when you modify result[i] based on str in your for loop


The length of str is 14, 12 characters plus 2 spaces.
The length of result is 12, since your regex only matches characters.
So when your for loop gets to i=4, where in the str it corresponds to a ’ ', after your rotation it becomes ‘{’. This is also why 2 characters are left out of your result array.

1 Like

Thanks A lot this really helped me

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