From 63a1f09aa4e5f457c9ace243786f387208578ff4 Mon Sep 17 00:00:00 2001 From: c-auri <43008483+c-auri@users.noreply.github.com> Date: Fri, 18 Nov 2022 15:44:57 +0100 Subject: [PATCH] Extract isLetter function for readability --- caesar/caesar.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/caesar/caesar.js b/caesar/caesar.js index 5839278..47a07bf 100644 --- a/caesar/caesar.js +++ b/caesar/caesar.js @@ -5,6 +5,10 @@ const caesar = function(string, shiftValue) { .join(""); }; +const isLetter = code => { + return (code >= 65 && code <= 90) || (code >= 97 && code <= 122); +} + // This function implements a version of the modulo operator // that returns the smallest positive remainder even for negative inputs. // See this link for details: @@ -14,7 +18,7 @@ const mod = (n, m) => (n % m + m) % m; const shift = (char, shiftValue) => { const code = char.charCodeAt(); - if ((code >= 65 && code <= 90) || (code >= 97 && code <= 122)) { + if (isLetter(code)) { const base = code < 97 ? 65 : 97; const shiftedCode = mod(code + shiftValue - base, 26) + base; return String.fromCharCode(shiftedCode);