Tell us what’s happening:
I think the .map method might be affecting how the charCodeAt and fromCharCode methods are behaving and I’m trying to find a solution.
The Regex doesn’t appear to be catching all letters and the 13 being added to each charcode doesn’t seem to be working as expected.
Any help would be much appreciated.
Your code so far
function rot13(str) {
let regex = /[A-Z]/g;
return Array.from(str).map(letter => regex.test(letter) ?
String.fromCharCode(letter.charCodeAt()+13).toUpperCase() : letter).join("");
}
// Change the inputs below to test
console.log(rot13("SERR CVMMN!"));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36.
I just realized that I had an extraneous /g flag on the regex, which was causing errors. Also, I didn’t realize the DEC values for letters have some symbols between upper and lower case. That’s why I created this to blindly add 13. I’m going to try and fix this now.