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.
This commit is contained in:
c-auri 2022-11-18 15:11:44 +01:00
parent 0d27422bc5
commit a539c1776a
1 changed files with 3 additions and 2 deletions

View File

@ -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;