2022-02-20 19:07:44 +00:00
|
|
|
const caesar = function (string, shift) {
|
2023-01-21 17:53:41 +00:00
|
|
|
return string
|
|
|
|
.split("")
|
|
|
|
.map((char) => shiftChar(char, shift))
|
|
|
|
.join("");
|
2022-02-20 19:07:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const codeSet = (code) => (code < 97 ? 65 : 97);
|
|
|
|
|
|
|
|
// this function is just a fancy way of doing % so that it works with negative numbers
|
|
|
|
// see this link for details:
|
|
|
|
// https://stackoverflow.com/questions/4467539/javascript-modulo-gives-a-negative-result-for-negative-numbers
|
|
|
|
const mod = (n, m) => ((n % m) + m) % m;
|
|
|
|
|
|
|
|
const shiftChar = (char, shift) => {
|
2023-01-21 17:53:41 +00:00
|
|
|
const code = char.charCodeAt();
|
2022-02-20 19:07:44 +00:00
|
|
|
|
2023-01-21 17:53:41 +00:00
|
|
|
if ((code >= 65 && code <= 90) || (code >= 97 && code <= 122)) {
|
|
|
|
return String.fromCharCode(
|
|
|
|
mod(code + shift - codeSet(code), 26) + codeSet(code)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return char;
|
2022-02-20 19:07:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = caesar;
|