Extract isLetter function for readability

This commit is contained in:
c-auri 2022-11-18 15:44:57 +01:00
parent 7beb49e593
commit 63a1f09aa4
1 changed files with 5 additions and 1 deletions

View File

@ -5,6 +5,10 @@ const caesar = function(string, shiftValue) {
.join(""); .join("");
}; };
const isLetter = code => {
return (code >= 65 && code <= 90) || (code >= 97 && code <= 122);
}
// This function implements a version of the modulo operator // This function implements a version of the modulo operator
// that returns the smallest positive remainder even for negative inputs. // that returns the smallest positive remainder even for negative inputs.
// See this link for details: // See this link for details:
@ -14,7 +18,7 @@ const mod = (n, m) => (n % m + m) % m;
const shift = (char, shiftValue) => { const shift = (char, shiftValue) => {
const code = char.charCodeAt(); const code = char.charCodeAt();
if ((code >= 65 && code <= 90) || (code >= 97 && code <= 122)) { if (isLetter(code)) {
const base = code < 97 ? 65 : 97; const base = code < 97 ? 65 : 97;
const shiftedCode = mod(code + shiftValue - base, 26) + base; const shiftedCode = mod(code + shiftValue - base, 26) + base;
return String.fromCharCode(shiftedCode); return String.fromCharCode(shiftedCode);