From 0d27422bc5376b76feef0d17261f770ee7aa3f38 Mon Sep 17 00:00:00 2001 From: c-auri <43008483+c-auri@users.noreply.github.com> Date: Fri, 18 Nov 2022 14:44:11 +0100 Subject: [PATCH] Change shift variable and function names Using shift as a parameter name might be confusing, since it is a verb and therefore not a good variable name on its own. Changing it to shiftValue should clear things up and also makes it possible to shorten the name of the shiftChar function to just shift, which leads to a cleaner signature: shift(char, shiftValue) --- caesar/caesar.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/caesar/caesar.js b/caesar/caesar.js index c2e926f..b6dcd95 100644 --- a/caesar/caesar.js +++ b/caesar/caesar.js @@ -1,7 +1,7 @@ -const caesar = function(string, shift) { +const caesar = function(string, shiftValue) { return string .split("") - .map(char => shiftChar(char, shift)) + .map(char => shift(char, shiftValue)) .join(""); }; @@ -12,12 +12,12 @@ const codeSet = code => (code < 97 ? 65 : 97); // 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) + mod(code + shiftValue - codeSet(code), 26) + codeSet(code) ); } return char;