0
var i = 1;
while(i < 100){
       i *= 2;
       document.write(i + ", ");
}

So it writes:

2, 4, 8, 16, 32, 64, 128,

What would the simplest way to take the comma off after it writes 128?

Code-Apprentice
  • 69,701
  • 17
  • 115
  • 226
Opie T
  • 3
  • 2
  • 1
    [You really shouldn't be using `document.write`](https://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice). – Terry Mar 17 '20 at 19:59

2 Answers2

1

What would the simplest way to take the comma off after it writes 128?

This simplest way to remove output is to not output it. One solution is to print the first value before the loop starts then write the comma before the value in the loop:

var i = 2;
document.write(i);
while(i < 99){
       i *= 2;
       document.write(", " + i);
}
Code-Apprentice
  • 69,701
  • 17
  • 115
  • 226
  • @OpieT Alternatively, you leave your original `document.write()` but change the loop to stop 1 sooner and write the last element after the loop. The details are left as an exercise to the reader. – Code-Apprentice Mar 17 '20 at 20:10
0

Do the comma as a prefix for everything but the first number

var i = 1; while(i < 100){
i *= 2;
document.write((i == 2 ? "" : ", ") + i);
}

So it writes:

2, 4, 8, 16, 32, 64, 128,

ControlAltDel
  • 28,815
  • 6
  • 42
  • 68