From a539c1776a20040d31b0fb498918631b7c195871 Mon Sep 17 00:00:00 2001 From: c-auri <43008483+c-auri@users.noreply.github.com> Date: Fri, 18 Nov 2022 15:11:44 +0100 Subject: [PATCH] Improve modulo comment Technically, the % operator in JS is _not_ the modulo operator. It's a different operator that returns negative values by design. So it _does_ work with negative numbers as intended, it's just not the operator that is needed for the caesar shift. Therefore, mod does not "make % work with negative numbers", it just uses % to implement the modulo operator. The previous comment might suggest that there is a bug that gets fixed, the new comment hopefully avoids that source of possible confusion. --- caesar/caesar.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/caesar/caesar.js b/caesar/caesar.js index b6dcd95..a4d935a 100644 --- a/caesar/caesar.js +++ b/caesar/caesar.js @@ -7,8 +7,9 @@ const caesar = function(string, shiftValue) { 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: +// 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;