这个 do while 循环有 break 是怎么回事?
What's going on in this do while loop with break?
我正在尝试了解带有 break
语句的 do/while 循环是如何工作的。我认为无论何时执行 break 语句(前两次迭代,因为 if 条件在那些时候为真但不是第三次),for 循环都会被转义,因此 n 不会递增。但是 console.log(`n is ${n}`)
行记录 n is 2
n is 2
n is 5
—— 它是如何从 2 跳到 5 的?我以为第三次会是 3(当 if 条件不成立时,因为 quot
是 60
而 60 % 2
确实等于 0,因此 break
语句未执行)。
// newArray = [5, 4, 3, 2, 1];
var quot = 0;
var loop = 1;
var n;
do {
quot = newArr[0] * loop * newArr[1];
for (n = 2; n < newArr.length; n++) {
if (quot % newArr[n] !== 0) {
break;
}
}
console.log(`n is ${n}`)
loop++;
} while (n !== newArr.length);
这是完整的代码(freeCodeCamp challenge 的解决方案):
function smallestCommons(arr) {
// Sort array from greater to lowest
// This line of code was from Adam Doyle (http://github.com/Adoyle2014)
arr.sort(function(a, b) {
return b - a;
});
// Create new array and add all values from greater to smaller from the
// original array.
var newArr = [];
for (var i = arr[0]; i >= arr[1]; i--) {
newArr.push(i);
}
// Variables needed declared outside the loops.
var quot = 0;
var loop = 1;
var n;
// Run code while n is not the same as the array length.
do {
quot = newArr[0] * loop * newArr[1];
for (n = 2; n < newArr.length; n++) {
if (quot % newArr[n] !== 0) {
break;
}
}
console.log(`n is ${n}`)
loop++;
} while (n !== newArr.length);
return quot;
}
// test here
smallestCommons([1,5]);
你的数组长度是5,所以如果你的if语句和break没有执行,那么n永远是5。
(n = 2; n < newArr.length; n++)
最后一次执行,n=4, n < newArr.length (which is 5), n++ (now n is 5)
就像你在问题中所说的那样"the if condition isn't true, since quot is 60 and 60 % 2 does equal 0, and therefore the break statement isn't executed"
您的 break 语句只会让您提前退出 for 循环,如果您的 for 循环完成它的完整循环,那么 n 将始终为 5。
希望这是有道理的!
我正在尝试了解带有 break
语句的 do/while 循环是如何工作的。我认为无论何时执行 break 语句(前两次迭代,因为 if 条件在那些时候为真但不是第三次),for 循环都会被转义,因此 n 不会递增。但是 console.log(`n is ${n}`)
行记录 n is 2
n is 2
n is 5
—— 它是如何从 2 跳到 5 的?我以为第三次会是 3(当 if 条件不成立时,因为 quot
是 60
而 60 % 2
确实等于 0,因此 break
语句未执行)。
// newArray = [5, 4, 3, 2, 1];
var quot = 0;
var loop = 1;
var n;
do {
quot = newArr[0] * loop * newArr[1];
for (n = 2; n < newArr.length; n++) {
if (quot % newArr[n] !== 0) {
break;
}
}
console.log(`n is ${n}`)
loop++;
} while (n !== newArr.length);
这是完整的代码(freeCodeCamp challenge 的解决方案):
function smallestCommons(arr) {
// Sort array from greater to lowest
// This line of code was from Adam Doyle (http://github.com/Adoyle2014)
arr.sort(function(a, b) {
return b - a;
});
// Create new array and add all values from greater to smaller from the
// original array.
var newArr = [];
for (var i = arr[0]; i >= arr[1]; i--) {
newArr.push(i);
}
// Variables needed declared outside the loops.
var quot = 0;
var loop = 1;
var n;
// Run code while n is not the same as the array length.
do {
quot = newArr[0] * loop * newArr[1];
for (n = 2; n < newArr.length; n++) {
if (quot % newArr[n] !== 0) {
break;
}
}
console.log(`n is ${n}`)
loop++;
} while (n !== newArr.length);
return quot;
}
// test here
smallestCommons([1,5]);
你的数组长度是5,所以如果你的if语句和break没有执行,那么n永远是5。
(n = 2; n < newArr.length; n++)
最后一次执行,n=4, n < newArr.length (which is 5), n++ (now n is 5)
就像你在问题中所说的那样"the if condition isn't true, since quot is 60 and 60 % 2 does equal 0, and therefore the break statement isn't executed"
您的 break 语句只会让您提前退出 for 循环,如果您的 for 循环完成它的完整循环,那么 n 将始终为 5。
希望这是有道理的!