Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS test if multiple of 10

Tags:

javascript

In a JS function using setIntervall, I want to perform a jquery animation every 10 loops (in the other 9 loops, other animations are being displayed).

I am using the variable i in my function and it increments +1 each loop. Is there a very easy way to check in javascript if i is a multiple of 10 (in order to perform my jquery animation)?

In PHP I would simply do if(($i % 10) == 0) ... but I didn't find it in JS.

like image 945
Adam Strudwick Avatar asked Aug 20 '11 02:08

Adam Strudwick


People also ask

How do you check if a number is a multiple of 10?

When you multiply a number by 10 you will get the number attached with zero as the result. eg: 10 × 2 = 20. Multiples of 10 are even numbers that end with 0. If a number is ending with 0 then it is always divisible by 10.

How do you check if something is a multiple in JS?

To check if one number is a multiple of another number, use the modulo operator % . The operator returns the remainder when one number is divided by another number. The remainder will only be zero if the first number is a multiple of the second.


2 Answers

Did you try it? I found a few sites that claim that the same operator % will work in JavaScript.

like image 113
Platinum Azure Avatar answered Oct 03 '22 01:10

Platinum Azure


The modulus operator in JS works just fine.

for (var ii=0; ii < 100; ii++)
{
    if (ii%10 == 0) console.log(ii);
}
like image 41
John Green Avatar answered Oct 03 '22 03:10

John Green