I have the following:
-1 mod 5
I expect to get 4
.
However in JS (-1 % 5
), I'm getting -1
.
What am I doing wrong?
I have the following:
-1 mod 5
I expect to get 4
.
However in JS (-1 % 5
), I'm getting -1
.
What am I doing wrong?
mod is the leftover when you divide the number by the divider.
In Math -1%5 = -1
so Js is working as expected.
You are not doing anything wrong. If you want to get a positive number (the difference) add the divider number to the result.
var result = (-1 % 5) + 5;
It doesn't matter where you add the number before the module or after. Its a pure math.