I've constructed a Caesar Cipher script down below, however, I wish for the output string to contain spaces and other characters. Despite my attempts with regex, it doesn't seem to rectify the issue or maybe I'm not utilizing it correctly – unsure of which.
Any assistance would be greatly appreciated. Thank you!
function caesarCipher(str, n) {
let newStr = '';
let alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('')
let regex = /[a-z]/
for (let i = 0; i < str.length; i++) {
if (str[i].match(regex) === false) {
newStr += str[i]
continue;
}
let currentIndex = alphabet.indexOf(str[i]);
let newIndex = currentIndex + n;
newStr += alphabet[newIndex];
}
return newStr
}
console.log(caesarCipher('ebiil tloia!', 3)) //should return hello world! but returns hellocworldc