2017-12-15 18:56:14 +00:00
|
|
|
const caesar = function(string, shift) {
|
|
|
|
return string
|
|
|
|
.split("")
|
|
|
|
.map(char => shiftChar(char, shift))
|
|
|
|
.join("");
|
|
|
|
};
|
2017-08-25 18:59:26 +00:00
|
|
|
|
2017-12-15 18:56:14 +00:00
|
|
|
const codeSet = code => (code < 97 ? 65 : 97);
|
2017-08-25 18:59:26 +00:00
|
|
|
|
2017-12-15 18:56:14 +00:00
|
|
|
const mod = (n, m) => (n % m + m) % m;
|
|
|
|
|
|
|
|
const shiftChar = (char, shift) => {
|
|
|
|
const code = char.charCodeAt();
|
|
|
|
|
|
|
|
if ((code >= 65 && code <= 90) || (code >= 97 && code <= 122)) {
|
|
|
|
return String.fromCharCode(
|
|
|
|
mod(code + shift - codeSet(code), 26) + codeSet(code)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return char;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = caesar;
|