+ 4
Someone explain me, how to work modulus ?
3 Réponses
+ 4
modulus is the remainder of division.
for instance 5/2 2 goes into 5 twice and leaves a remainder of 1 so
5%2 is 1
if we had 19/5, 5 goes into 19 3 times and leaves a remainder of 4 so
19%5 is 4
+ 2
modulu is reminder and addition
example
37%5=2. with rest of 37-(5*7)=2
+ 1
It is good when trying to split numbers into categories. For example, seconds into hours, minutes and seconds.
var time = 1253735;
//seconds
var hours =
Math.floor(time / (24 * 60 * 60));
var minutes =
Math.floor(time % (24 * 60 * 60) / (60 * 60));
var seconds =
time % (24 * 60 * 60) % ( 60 * 60);