From 560c8b227022f51789a2ffa8914f5d0d5bd28f25 Mon Sep 17 00:00:00 2001 From: Juju1980 <88070381+Juju1980@users.noreply.github.com> Date: Sat, 28 Aug 2021 08:57:29 -0400 Subject: [PATCH] Update caesar.js Caison Lee --- tasks/5. Caesar cipher/caesar.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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; + }, {}); +};