【发布时间】:2014-11-27 11:21:48
【问题描述】:
我正在为一个项目编写一个 for 循环,该循环提示用户输入一个数字并不断提示,不断地将数字相加。当一个字符串被引入时,循环应该停止。我用一个while循环完成了它,但项目声明我们也必须用一个for循环来完成它。问题是即使'a = false',提示也会继续运行。有人能解释一下javascript的思维过程吗?我想了解为什么即使不满足条件,它也会继续循环运行。谢谢
var addSequence2 = function() {
var total = 0;
var a;
for (; a = true; ) {
var input = prompt("Your current score is " +total+ "\n" + "Next number...");
if (!isNaN(input)) {
a = true;
total = +total + +input;
}
else if (isNaN(input)) {
a = false;
document.write("Your total is " + total);
}
}
};
【问题讨论】: