So I’m doing Caeser’s Cipher from the JavaScript projects.
At first I thought I had it with this:
function rot13(str) { // LBH QVQ VG!
let arr = str.split('');
let newArr =[];
for (let i = 0; i < str.length; i++) {
let newArr = arr.map((item) => item.charCodeAt(i) - 13);
return newArr.map((item) => String.fromCharCode(item)).join('')
}
}
but then I realised that only worked with half the letters, so I tried this (probably gobbledegook):
function rot13(str) { // LBH QVQ VG!
let arr = str.split('');
let newArr =[];
for (let i = 0; i < str.length; i++) {
let newArr = arr.map((item) => {
if (item.charCodeAt(i) >= 78) {
item = item.charCodeAt(i) - 13;
}
else {
item = item.charCodeAt(i) + 13;
}
})
return newArr;
}
}
I’m trying to do two different things to the items depending on where they come in the alphabet but I can’t find the syntax to do this. This comes back as an array of undefineds.
Pls help! Thanks