Tell us what’s happening:
When I do calculate the UTF-16 code - 13 some letters do not match, for example the R becomes an 8 I also can’t figure out how to modify only the A-Z characters from the string
Your code so far
function rot13(str) {
let i = 0
for (let l of str){
let x = str.charCodeAt([i])
i++
console.log(String.fromCharCode(x - 13))
}
return str;
}
rot13("SERR PBQR PNZC");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0.
Hello there,
I would recommend a different approach to this challenge:
-control your input
convert all the characters in the string to lowercase and make sure you don’t have any unwanted symbols.
If needed you can always change the back to uppercase at the end.
-use your own array of valid characters
It’s the best way to make sure you get all the correct characters.
Here’s a great example of caesars cipher function:
function caesarCipther(str, num) {
num = num % 26;
const lowerCaseString = str.toLowerCase();
const charArr = 'abcdefghijklmnopqrstuvwxyz';
let newStr = '';
for (var i = 0; i < lowerCaseString.length; i++) {
const char = lowerCaseString[i];
if (char === " ") {
newStr += char;
continue
}
const currIdx = charArr.indexOf(char);
let newIdx = currIdx + num;
if (newIdx > 25) newIdx -= 26;
if (newIdx < 0) newIdx += 26;
if (str[i] === str[i].toUpperCase()) {
newStr += charArr[newIdx].toUpperCase()
}
else newStr += charArr[newIdx];
}
console.log(newStr)
}