2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
id: 5675e877dbd60be8ad28edc6
|
2021-03-15 03:20:39 +00:00
|
|
|
|
title: 使用 For 循环遍历数组
|
2018-10-10 22:03:03 +00:00
|
|
|
|
challengeType: 1
|
2020-04-29 10:29:13 +00:00
|
|
|
|
videoUrl: 'https://scrimba.com/c/caeR3HB'
|
|
|
|
|
forumTopicId: 18216
|
2021-01-13 02:31:00 +00:00
|
|
|
|
dashedName: iterate-through-an-array-with-a-for-loop
|
2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --description--
|
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
JavaScript 中的一个常见任务是遍历数组的内容。 一种方法是使用 `for` 循环。 下面的代码将输出数组 `arr` 的每个元素到控制台:
|
2020-04-29 10:29:13 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
var arr = [10, 9, 8, 7, 6];
|
|
|
|
|
for (var i = 0; i < arr.length; i++) {
|
|
|
|
|
console.log(arr[i]);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2021-05-09 19:42:02 +00:00
|
|
|
|
记住数组的索引从零开始的,这意味着数组的最后一个元素的下标是:`length - 1`(数组的长度 -1)。 我们这个循环的条件是 `i < arr.length`,当 `i` 的值为 `length` 的时候循环就停止了。 在这个例子中,最后一个循环是 `i === 4`,也就是说,当 `i` 的值等于 `arr.length - 1` 时,结果输出 `6`。 然后 `i` 增加到 `5`,循环会终止,因为 `i < arr.length` 是 `false`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --instructions--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
声明并初始化一个变量 `total` 值为 `0`。 使用 `for` 循环,使得 `total` 的值为 `myArr` 的数组中的每个元素的值的总和。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --hints--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
`total` 应该被声明, 并且初始化值为 0。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
2021-02-06 04:42:36 +00:00
|
|
|
|
assert(code.match(/(var|let|const)\s*?total\s*=\s*0.*?;?/));
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
`total` 应该等于 20。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
|
assert(total === 20);
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
你应该使用 `for` 循环在 `myArr` 中遍历。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
|
|
```js
|
2021-02-06 04:42:36 +00:00
|
|
|
|
assert(/for\s*\(/g.test(code) && /myArr\s*\[/g.test(code));
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```
|
2020-04-29 10:29:13 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
不能直接把 `total` 设置成 20。
|
2020-04-29 10:29:13 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
2021-02-06 04:42:36 +00:00
|
|
|
|
assert(!__helpers.removeWhiteSpace(code).match(/total[=+-]0*[1-9]+/gm));
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
2020-04-29 10:29:13 +00:00
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --after-user-code--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
(function(){if(typeof total !== 'undefined') { return "total = " + total; } else { return "total is undefined";}})()
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
// Setup
|
|
|
|
|
var myArr = [ 2, 3, 4, 5, 6];
|
|
|
|
|
|
|
|
|
|
// Only change code below this line
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
```js
|
|
|
|
|
var myArr = [ 2, 3, 4, 5, 6];
|
|
|
|
|
var total = 0;
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < myArr.length; i++) {
|
|
|
|
|
total += myArr[i];
|
|
|
|
|
}
|
|
|
|
|
```
|