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
  • 188,820
  • 38
  • 367
  • 383
Azib Yaqoob
  • 37
  • 2
  • 5
  • Perhaps its worth a read of this question -> http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice – Undefined Jun 13 '12 at 11:01

3 Answers3

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

http://jsfiddle.net/kX8hn/

CD..
  • 65,131
  • 24
  • 138
  • 151
4
for (var i=1; i <= 100; i += 2)
    document.write(i + "<br />");
Danilo Valente
  • 10,599
  • 8
  • 49
  • 65
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.

Andrew Leach
  • 12,634
  • 1
  • 38
  • 47