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.

1

3 Answers 3

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

http://jsfiddle.net/kX8hn/

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 />");

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.

Comments

Your Answer

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.