1

I have this variable:

var number = 0;

And this number is increasing all the time. Everytime the number has increased with 200 i want to run a specific code. Atm my code is looking something similar to this:

if(number == 200 || number == 400 || number == 600 || number == 800 || number == 1000){
//run some code
};

Since the number should, in theory, be able to increase infinitely i wouldn't want to compare the value like this.

Question: how to(if possible) check if the variable is equal to all values in this specific mathematical order(every 200 number).

Mattias
  • 2,117
  • 5
  • 20
  • 43
  • 1
    possible duplicate of [What does % do in javascript?](http://stackoverflow.com/questions/8900652/what-does-do-in-javascript) – Mikk3lRo Jul 16 '15 at 10:15

3 Answers3

6

You can use remainder modulus operator % , dividing number with 200 and getting zero as remainder will give you numbers multiple of 200

if(number % 200 === 0)

if you want to limit from number 200 and 1000 then you can put other condition

if(number % 200 === 0 && number >= 200 && number <= 1000)
Adil
  • 139,325
  • 23
  • 196
  • 197
  • It's also need to check if the number in range `[200, 1000]` – hindmost Jul 16 '15 at 10:17
  • @hindmost `if(number > 200 && number < 1000 && number % 200 === 0)` – Tushar Jul 16 '15 at 10:18
  • @hindmost No, it doesn't. The OP has coded it up to 1000 so far, but doesn't want to have to keep checking the values beyond that: _"the number should, in theory, be able to increase infinitely i wouldn't want to compare the value like this."_ – James Thorpe Jul 16 '15 at 10:20
  • Note that [the MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Remainder_()) talks about this being the "remainder operator" now, with a link to a proposal to add a true modulus operator – James Thorpe Jul 16 '15 at 10:21
  • @Adil `200 and number`, shouldn't it be `&&` – Tushar Jul 16 '15 at 10:22
0

You can use the Modulus operator, what this does is takes a number and returns the remainder

11 % 3 = 2 Remainer

So what you can do is

if(number % 200 === 0)
{
    // Code goes here
} 
Canvas
  • 5,343
  • 7
  • 48
  • 93
0

Maybe you can use the modulus operator and check for a 0 result

if(number%200 == 0) {
    //run some code
    }
ElPedro
  • 536
  • 9
  • 15