Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions tasks/5. Caesar cipher/caesar.js
Original file line number Diff line number Diff line change
@@ -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;
}, {});
};