这些运算符如何影响这个简单的 javascript 程序?
How are these operators affecting this simple javascript program?
这可能非常明显,但我似乎无法理解:我已经研究了所有这些运算符的含义并注释掉了我认为它们正在做的事情。显然总数 = 55。我认为它会 = 11。我哪里错了?非常感谢。
var total = 0;
var count = 1;
while (count <= 10) { // while "count" is less than or equal to 10 do...
total += count; // total = total + count (total = 0 + count)
count += 1; // count = 1 + 1 (adding 1 to count every loop until count is equal to 11)
};
console.log(total); -> 55
// total is 0 + count
// when the program ends count = 11
// 0 + 11 = 11
要了解发生了什么,请尝试编写:
console.log(total);
console.log(count);
while 循环体内。
您将看到它在每次迭代中的表现。
count += 1;
表示:
count = count + 1;
就像其他人在评论中所说的那样。您正在递增 count
和 total
。做一个或另一个。
这是您的代码当前正在执行的操作:
var total = 0;
var count = 1;
var iteration = 0;
var div = document.getElementById("div");
while (count <= 10) {
total += count;
count += 1;
iteration++;
div.innerHTML += "Iteration " + iteration + ": ";
div.innerHTML += "count = " + count + "; ";
div.innerHTML += "total = " + total + ";<br /><br />"
};
<div id="div"></div>
有几种方法可以做到这一点。
一种方法是在每次迭代时将计数和总数递增 1:
var total = 0;
var count = 1;
while (count <= 10) { // while "count" is less than or equal to 10 do...
total++ // Increase the total by 1
count++ // Increase the count by 1
};
document.getElementById("result").innerHTML = total
<div id="result"></div>
但这只会给您 10
的结果,因为您的计数从 1
开始
另一种方法是只增加计数,然后将计数分配给循环外的总数:
var total = 0;
var count = 1;
while (count <= 10) { // while "count" is less than or equal to 10 do...
count++ // Increase the count by 1
};
total = count;
document.getElementById("result").innerHTML = total
<div id="result"></div>
或者,完全删除 count
:
var total = 0;
while (total <= 10) {
total++;
};
document.getElementById("result").innerHTML = total
<div id="result"></div>
或者,我的首选方法是将循环从 while
更改为 for
循环
var total = 0;
for (var i = 0; i <= 10; i++) {
total++;
}
document.getElementById("result").innerHTML = total
<div id="result"></div>
这可能非常明显,但我似乎无法理解:我已经研究了所有这些运算符的含义并注释掉了我认为它们正在做的事情。显然总数 = 55。我认为它会 = 11。我哪里错了?非常感谢。
var total = 0;
var count = 1;
while (count <= 10) { // while "count" is less than or equal to 10 do...
total += count; // total = total + count (total = 0 + count)
count += 1; // count = 1 + 1 (adding 1 to count every loop until count is equal to 11)
};
console.log(total); -> 55
// total is 0 + count
// when the program ends count = 11
// 0 + 11 = 11
要了解发生了什么,请尝试编写:
console.log(total);
console.log(count);
while 循环体内。
您将看到它在每次迭代中的表现。
count += 1;
表示:
count = count + 1;
就像其他人在评论中所说的那样。您正在递增 count
和 total
。做一个或另一个。
这是您的代码当前正在执行的操作:
var total = 0;
var count = 1;
var iteration = 0;
var div = document.getElementById("div");
while (count <= 10) {
total += count;
count += 1;
iteration++;
div.innerHTML += "Iteration " + iteration + ": ";
div.innerHTML += "count = " + count + "; ";
div.innerHTML += "total = " + total + ";<br /><br />"
};
<div id="div"></div>
有几种方法可以做到这一点。
一种方法是在每次迭代时将计数和总数递增 1:
var total = 0;
var count = 1;
while (count <= 10) { // while "count" is less than or equal to 10 do...
total++ // Increase the total by 1
count++ // Increase the count by 1
};
document.getElementById("result").innerHTML = total
<div id="result"></div>
但这只会给您 10
的结果,因为您的计数从 1
另一种方法是只增加计数,然后将计数分配给循环外的总数:
var total = 0;
var count = 1;
while (count <= 10) { // while "count" is less than or equal to 10 do...
count++ // Increase the count by 1
};
total = count;
document.getElementById("result").innerHTML = total
<div id="result"></div>
或者,完全删除 count
:
var total = 0;
while (total <= 10) {
total++;
};
document.getElementById("result").innerHTML = total
<div id="result"></div>
或者,我的首选方法是将循环从 while
更改为 for
循环
var total = 0;
for (var i = 0; i <= 10; i++) {
total++;
}
document.getElementById("result").innerHTML = total
<div id="result"></div>