function rot13(str) {
let arr=str.split("");
let regex=/[A-Z]/;
for(let i in str){
let replaceWith=str[i].match(regex)?String.fromCharCode(
str.charCodeAt(i)-78 < 0 ? str.charCodeAt(i)+13 :str.charCodeAt(i)-13
):str[i];
console.log(str[i],replaceWith)
arr[i]=replaceWith;
}
return arr.join("");
}
console.log(rot13("SERR PBQR PNZC"));
You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.
does this answer your question of what’s going wrong?
The replace() method returns a new string with some or all matches of a pattern replaced by a replacement . […] If pattern is a string, only the first occurrence will be replaced.