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
  • 202,337
  • 40
  • 393
  • 406
Azib Yaqoob
  • 37
  • 1
  • 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..
  • 72,281
  • 25
  • 154
  • 163
4
for (var i=1; i <= 100; i += 2)
    document.write(i + "<br />");
Danilo Valente
  • 11,270
  • 8
  • 53
  • 67
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,945
  • 1
  • 40
  • 47