views:

65

answers:

3

I was looking to create a boolean test for a number when it is 10, 20, 30, 40. This would be used in a loop, 1 to 100. ParseInt seems a bit part but was wondering what a method for a true or false answer maybe.

+7  A: 

How about something like:

for ( var i = 0; i <= 100; ++i) {
  if (i % 10 == 0) {
    // Something here for multiples of 10
  } else {
    // Something else here.
  }
}
g.d.d.c
+1  A: 

If I understand your question correctly, you want to do the following:

for(int i = 1; i <= 100; ++i)
{
  if(i % 10 == 0)
   { 
    //success
   }

}

Then you want to use the modulus operator (%) which returns the remainder of any division. So x % 10 will be 0 for values of x that are multiples of 10.

Alan
+1  A: 
for (var i = 1; i <= 100; i++) {
  // Good catch Gert - ! has higher precedence than %, needs parens
  if (!(i % 10)) {
    alert(i);
  }
}

Will alert i every time i is divisible by 10, 20, 30, 40, 50, 60 etc.

Hooray Im Helping
Fix the typos and this will be the best solution. for (var i=1;i<=100;i++) { if (!(i % 10)) { alert(i); } }
Gert G
Good catches Gert.
Hooray Im Helping