2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 56104e9e514f539506016a5c
|
|
|
|
|
challengeType: 1
|
2020-04-29 18:29:13 +08:00
|
|
|
|
videoUrl: 'https://scrimba.com/c/cm8n7T9'
|
|
|
|
|
forumTopicId: 18212
|
2020-10-01 17:54:21 +02:00
|
|
|
|
title: 使用 For 循环遍历数组的奇数
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## Description
|
2020-04-29 18:29:13 +08:00
|
|
|
|
<section id='description'>
|
|
|
|
|
for循环可以按照我们指定的顺序来迭代,通过更改我们的<code>计数器</code>,我们可以按照偶数顺序来迭代。
|
|
|
|
|
初始化<code>i = 0</code>,当<code>i < 10</code>的时候继续循环。
|
|
|
|
|
<code>i += 2</code>让<code>i</code>每次循环之后增加2。
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
var ourArray = [];
|
|
|
|
|
for (var i = 0; i < 10; i += 2) {
|
|
|
|
|
ourArray.push(i);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
循环结束后,<code>ourArray</code>的值为<code>[0,2,4,6,8]</code>。
|
|
|
|
|
改变<code>计数器</code>,这样我们可以用奇数来数。
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
## Instructions
|
2020-04-29 18:29:13 +08:00
|
|
|
|
<section id='instructions'>
|
|
|
|
|
写一个<code>for</code>循环,把从 1 到 9 的奇数添加到<code>myArray</code>。
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
|
|
```yml
|
|
|
|
|
tests:
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: 你应该使用<code>for</code>循环。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(code.match(/for\s*\(/g).length > 1);
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: <code>myArray</code>应该等于<code>[1,3,5,7,9]</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert.deepEqual(myArray, [1,3,5,7,9]);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
// Example
|
|
|
|
|
var ourArray = [];
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < 10; i += 2) {
|
|
|
|
|
ourArray.push(i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Setup
|
|
|
|
|
var myArray = [];
|
|
|
|
|
|
|
|
|
|
// Only change code below this line.
|
|
|
|
|
|
2020-04-29 18:29:13 +08:00
|
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### After Test
|
|
|
|
|
<div id='js-teardown'>
|
|
|
|
|
|
|
|
|
|
```js
|
2020-04-29 18:29:13 +08:00
|
|
|
|
if(typeof myArray !== "undefined"){(function(){return myArray;})();}
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
2020-04-29 18:29:13 +08:00
|
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```js
|
2020-04-29 18:29:13 +08:00
|
|
|
|
var ourArray = [];
|
|
|
|
|
for (var i = 0; i < 10; i += 2) {
|
|
|
|
|
ourArray.push(i);
|
|
|
|
|
}
|
|
|
|
|
var myArray = [];
|
|
|
|
|
for (var i = 1; i < 10; i += 2) {
|
|
|
|
|
myArray.push(i);
|
|
|
|
|
}
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
2020-04-29 18:29:13 +08:00
|
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
|
</section>
|