2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 587d7b86367417b2b2512b3c
|
2020-12-16 00:37:30 -07:00
|
|
|
title: 重新初始化循环中的变量时要小心
|
2018-10-10 18:03:03 -04:00
|
|
|
challengeType: 1
|
2020-09-07 16:09:54 +08:00
|
|
|
forumTopicId: 301194
|
2021-01-13 03:31:00 +01:00
|
|
|
dashedName: use-caution-when-reinitializing-variables-inside-a-loop
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --description--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
有时需要在循环中保存信息以增加计数器或重置变量。一个潜在的问题是变量什么时候该重新初始化,什么时候不该重新初始化,反之亦然。如果你不小心重置了用于终止条件的变量,导致无限循环,这将特别危险。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
使用`console.log()`在每个循环中打印变量值可以发现与重置相关的错误或者重置变量失败。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --instructions--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
以下函数应该创建一个具有`m`行和`n`列“零”的二维数组。不幸的是,它没有产生预期的输出,因为`row`变量没有在外部循环中重新初始化(设置回空数组)。修改代码,使其正确地返回包含 3 行 2 列“零”的二维数组,即`[[0, 0], [0, 0], [0, 0]]`。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --hints--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
你应将变量`matrix`设置为 3 行 2 列“零”的二维数组。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
assert(JSON.stringify(matrix) == '[[0,0],[0,0],[0,0]]');
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
变量`matrix`应有 3 行。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
assert(matrix.length == 3);
|
|
|
|
```
|
2020-09-07 16:09:54 +08:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
变量`matrix`每行应有 2 列。
|
2020-09-07 16:09:54 +08:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
matrix[0].length == 2 && matrix[1].length === 2 && matrix[2].length === 2
|
|
|
|
);
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
2020-08-13 17:24:35 +02:00
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
|
|
|
function zeroArray(m, n) {
|
|
|
|
// Creates a 2-D array with m rows and n columns of zeroes
|
|
|
|
let newArray = [];
|
|
|
|
let row = [];
|
|
|
|
for (let i = 0; i < m; i++) {
|
|
|
|
// Adds the m-th row into newArray
|
|
|
|
|
|
|
|
for (let j = 0; j < n; j++) {
|
|
|
|
// Pushes n zeroes into the current row to create the columns
|
|
|
|
row.push(0);
|
|
|
|
}
|
|
|
|
// Pushes the current row, which now has n zeroes in it, to the array
|
|
|
|
newArray.push(row);
|
|
|
|
}
|
|
|
|
return newArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
let matrix = zeroArray(3, 2);
|
|
|
|
console.log(matrix);
|
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --solutions--
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
```js
|
|
|
|
function zeroArray(m, n) {
|
|
|
|
// Creates a 2-D array with m rows and n columns of zeroes
|
|
|
|
let newArray = [];
|
|
|
|
for (let i = 0; i < m; i++) {
|
|
|
|
let row = [];
|
|
|
|
// Adds the m-th row into newArray
|
|
|
|
|
|
|
|
for (let j = 0; j < n; j++) {
|
|
|
|
// Pushes n zeroes into the current row to create the columns
|
|
|
|
row.push(0);
|
|
|
|
}
|
|
|
|
// Pushes the current row, which now has n zeroes in it, to the array
|
|
|
|
newArray.push(row);
|
|
|
|
}
|
|
|
|
return newArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
let matrix = zeroArray(3, 2);
|
|
|
|
console.log(matrix);
|
|
|
|
```
|