2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 5675e877dbd60be8ad28edc6
|
|
|
|
|
title: Iterate Through an Array with a For Loop
|
|
|
|
|
challengeType: 1
|
|
|
|
|
videoUrl: ''
|
|
|
|
|
localeTitle: 使用For循环遍历数组
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## Description
|
|
|
|
|
<section id="description"> JavaScript中的一个常见任务是遍历数组的内容。一种方法是使用<code>for</code>循环。此代码将数组<code>arr</code>每个元素输出到控制台: <blockquote> var arr = [10,9,8,7,6]; <br> for(var i = 0; i <arr.length; i ++){ <br> (ARR [I])的console.log; <br> } </blockquote>请记住,数组具有从零开始的编号,这意味着数组的最后一个索引是长度 - 1.我们对此循环的<dfn>条件</dfn>是<code>i < arr.length</code> ,当<code>i</code>长度为1时停止。 </section>
|
|
|
|
|
|
|
|
|
|
## Instructions
|
|
|
|
|
<section id="instructions">声明并将变量<code>total</code>初始化为<code>0</code> 。使用<code>for</code>循环将<code>myArr</code>数组的每个元素的值添加到<code>total</code> 。 </section>
|
|
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
|
|
```yml
|
|
|
|
|
tests:
|
|
|
|
|
- text: 应声明<code>total</code>并初始化为0
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(code.match(/(var|let|const)\s*?total\s*=\s*0.*?;?/));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
- text: <code>total</code>应该等于20
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(total === 20);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
- text: 您应该使用<code>for</code>循环来遍历<code>myArr</code>
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(code.match(/for\s*\(/g).length > 1 && code.match(/myArr\s*\[/));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
- text: 不要直接将<code>total</code>设置为20
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(!code.replace(/\s/g, '').match(/total[=+-]0*[1-9]+/gm));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
// Example
|
|
|
|
|
var ourArr = [ 9, 10, 11, 12];
|
|
|
|
|
var ourTotal = 0;
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < ourArr.length; i++) {
|
|
|
|
|
ourTotal += ourArr[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Setup
|
|
|
|
|
var myArr = [ 2, 3, 4, 5, 6];
|
|
|
|
|
|
|
|
|
|
// Only change code below this line
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### After Test
|
|
|
|
|
<div id='js-teardown'>
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
console.info('after the test');
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
// solution required
|
|
|
|
|
```
|
|
|
|
|
</section>
|