正如你看到我的代码,我在 2-3 次后在 while 循环中输入一个数字,但它没有返回任何值或表格。我应该显示输入的数字表。当我第一次尝试输入数字时,它会返回表格,但在第 3-4 次尝试中,它只接受该值,但不返回任何表格。<script type="text/javascript"> var num = prompt('Enter a number for which you want to display the table'); if(num.length === 0){ while(num.length === 0){ var num = prompt('Enter a number for which you want to display the table'); } }else{ num2 = Number(num); for(var i = 1; i <= 10; i++){ document.write(num2 * i + '<br>'); } }</script>
1 回答
largeQ
TA贡献2039条经验 获得超7个赞
您不需要if声明,您需要解析num为number
这应该有效:
let num = 0;
while (num === 0) {
num = +prompt('Enter a number for which you want to display the table');
}
num2 = Number(num);
for (var i = 1; i <= 10; i++) {
document.write(num2 * i + '<br>');
}
添加回答
举报
0/150
提交
取消