Write odd numbers from 1 to 100.. And I used this piece of code for this:
var i=1;
for (i=1; i < 51; i = i + 1){
document.write(i -1 + i );
document.write("<br />");
}
Now, can somebody please tell me if this code is right or I can make it better.
Mat
208k41 gold badges409 silver badges423 bronze badges
asked Jun 13, 2012 at 11:00
Azib Yaqoob
371 gold badge2 silver badges5 bronze badges
-
Perhaps its worth a read of this question -> stackoverflow.com/questions/802854/…Undefined– Undefined2012年06月13日 11:01:44 +00:00Commented Jun 13, 2012 at 11:01
3 Answers 3
for (var i=1; i < 100; i += 2){
document.write(i);
document.write("<br />");
}
answered Jun 13, 2012 at 11:01
CD..
74.4k25 gold badges160 silver badges170 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Barry Kaye
This will write odd numbers up to and inc. 49 not up to 100 as the OP asks.
Azib Yaqoob
Can you please tell me, why can't I use i < 100 for this code? Like when I did it, the results were shown 1 to 200 odd numbers.
for (var i=1; i <= 100; i += 2)
document.write(i + "<br />");
answered Jun 13, 2012 at 11:01
Danilo Valente
11.4k8 gold badges55 silver badges71 bronze badges
1 Comment
Barry Kaye
This will write odd numbers up to and inc. 49 not up to 100 as the OP asks.
It's wrong and you can make it better. Start at 1 and count up to 100 in increments of 2:
for (var i=1; i < 100; i += 2){
document.write(i);
document.write("<br />");
}
The usual caveats about document.write() apply.
answered Jun 13, 2012 at 11:03
Andrew Leach
13k1 gold badge44 silver badges48 bronze badges
Comments
lang-js