I'm doing a kata that decodes a caesar cipher string into readable text. I'm using RegEx within a map to find special characters and skip over them, but the output is flaky if I have two or more special characters next to each other ', ' or ' :) '. It seems to skip over some special characters.
Can anyone explain what's going on?
I haven't included the changeCharCode function code because I think the issue is in my map.
function decodeString(string) {
const stringArr = string.toLowerCase().split('');
const specialCharacters = /[ .,\/#!$%\^&\*;:{}=\-_`~()]/g;
const codeOfX = 'x'.charCodeAt(0);
const codeOfLastLetter = stringArr[stringArr.length - 1].charCodeAt(0);
const codeShift = codeOfX - codeOfLastLetter;
return stringArr.map((elem) => {
// Special character treatment
return specialCharacters.test(elem) === true ? elem : changecharCode(elem, codeShift);
}).join('').toUpperCase();
}
function changecharCode (letter, codeShift) {
const currentCode = letter.charCodeAt(0);
// Uppercase letters
if ((currentCode >= 65) && (currentCode <= 90))
return letter = String.fromCharCode(((currentCode - 65 + codeShift) % 26) + 65);
// Lowercase letters
else if ((currentCode >= 97) && (currentCode <= 122))
return letter = String.fromCharCode(((currentCode - 97 + codeShift) % 26) + 97);
}
decodeString(' :) ') => ' ) '
decodeString(', ') => ','