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
2010-06-15 16:22:47
+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
2010-06-15 16:23:48
+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
2010-06-15 16:24:24
Fix the typos and this will be the best solution. for (var i=1;i<=100;i++) { if (!(i % 10)) { alert(i); } }
Gert G
2010-06-15 23:11:46
Good catches Gert.
Hooray Im Helping
2010-06-16 17:28:14