From 77c5d64b37f1ca4b811a948da1a6d172976a7f1e Mon Sep 17 00:00:00 2001 From: c-auri <43008483+c-auri@users.noreply.github.com> Date: Sat, 18 Mar 2023 23:32:20 +0100 Subject: [PATCH] Alter caesar solution with clean code practices (#302) --- caesar/caesar.js | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/caesar/caesar.js b/caesar/caesar.js index c2e926f..47a07bf 100644 --- a/caesar/caesar.js +++ b/caesar/caesar.js @@ -1,24 +1,27 @@ -const caesar = function(string, shift) { +const caesar = function(string, shiftValue) { return string .split("") - .map(char => shiftChar(char, shift)) + .map(char => shift(char, shiftValue)) .join(""); }; -const codeSet = code => (code < 97 ? 65 : 97); +const isLetter = code => { + return (code >= 65 && code <= 90) || (code >= 97 && code <= 122); +} -// this function is just a fancy way of doing % so that it works with negative numbers -// see this link for details: +// This function implements a version of the modulo operator +// that returns the smallest positive remainder even for negative inputs. +// 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) => { +const shift = (char, shiftValue) => { const code = char.charCodeAt(); - if ((code >= 65 && code <= 90) || (code >= 97 && code <= 122)) { - return String.fromCharCode( - mod(code + shift - codeSet(code), 26) + codeSet(code) - ); + if (isLetter(code)) { + const base = code < 97 ? 65 : 97; + const shiftedCode = mod(code + shiftValue - base, 26) + base; + return String.fromCharCode(shiftedCode); } return char; };