0
我不明白这段代码。while循环中声明变量javascript
var timesTable;
while ((timesTable = prompt("Enter the times table", -1)) != -1) {.....}
为什么有必要先声明变量?我试图做到这一点:
while ((var timesTable = prompt("Enter the times table", -1)) != -1) {.....}
但它不起作用。有什么问题?是关于范围的东西?
完整的程序是:通过提前
function writeTimesTable(timesTable, timesByStart, timesByEnd) {
for (; timesByStart <= timesByEnd; timesByStart++) {
document.write(timesTable + " * " + timesByStart + " = " + timesByStart * timesTable + "<br />");
}
}
var timesTable;
while ((timesTable = prompt("Enter the times table", -1)) != -1) {
while (isNaN(timesTable) == true) {
timesTable = prompt(timesTable + " is not a " + "valid number, please retry", -1);
}
document.write("<br />The " + timesTable + " times table<br />");
writeTimesTable(timesTable, 1, 12);
}
感谢。
定义的原因“不工作”。 – deceze