0

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
1

3 Answers 3

9
for (var i=1; i < 100; i += 2){
 document.write(i);
 document.write("<br />");
}

http://jsfiddle.net/kX8hn/

answered Jun 13, 2012 at 11:01
Sign up to request clarification or add additional context in comments.

2 Comments

This will write odd numbers up to and inc. 49 not up to 100 as the OP asks.
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.
4
for (var i=1; i <= 100; i += 2)
 document.write(i + "<br />");
answered Jun 13, 2012 at 11:01

1 Comment

This will write odd numbers up to and inc. 49 not up to 100 as the OP asks.
2

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.