diff --git a/tasks/5. Caesar cipher/caesar.js b/tasks/5. Caesar cipher/caesar.js index 30667d1..f57b392 100644 --- a/tasks/5. Caesar cipher/caesar.js +++ b/tasks/5. Caesar cipher/caesar.js @@ -1,3 +1,21 @@ export function encryptCaesar(inputString, key) { // TODO: write your code here + const legend = 'abcdefghijklmnopqrstuvwxyz'.split(''); + const map = getMap(legend, key); + return inputString + .toLowerCase() + .split('') + .map(char => map[char] || char) + .join(''); } +const getMap = (legend, shift) => { + return legend.reduce((charsMap, currentChar, charIndex) => { + const copy = { ...charsMap }; + let ind = (charIndex + shift) % legend.length; + if (ind < 0) { + ind += legend.length; + }; + copy[currentChar] = legend[ind]; + return copy; + }, {}); +};