...
const string = 'hello! how are you doing?';
const rotation = 5;
const alphabet = 'abcdefghijklmnopqrstuvwxyz';
const alphabetMap = alphabet.split('').reduce((map, curr, idx) => {
map[curr] = idx;
return map;
}, {});
...
function getPosUp(pos) {
return (pos === alphabet.length - 1) ? 0 : pos + 1;
}
function getPosDown(pos) {
return (pos === 0) ? alphabet.length - 1 : pos - 1;
}
function getNextChar(currChar, direction) {
const pos = alphabetMap[currChar];
const nextPos = direction === 'up' ? getPosUp(pos) : getPosDown(pos);
const nextChar = alphabet.charAt(nextPos);
...
return nextChar;
}
function cipher(str, rotation, direction, cipherTracer) {
if (!str) return '';
for (let i = 0; i < str.length; i++) {
...