简单 - 从 While 循环的输出末尾删除逗号

Simple- Remove comma from end of output of While Loop

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

所以写成:

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

写128后逗号去掉最简单的方法是什么?

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

删除输出的最简单方法是输出它。一个解决方案是在循环开始之前打印第一个值,然后在循环中的值之前写上逗号:

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

除第一个数字外,所有内容都使用逗号作为前缀

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

所以它写道:

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