2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: cf1111c1c11feddfaeb1bdef
|
|
|
|
|
title: Iterate with JavaScript While Loops
|
|
|
|
|
challengeType: 1
|
|
|
|
|
videoUrl: ''
|
|
|
|
|
localeTitle: 在循环时使用JavaScript进行迭代
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## Description
|
|
|
|
|
<section id="description">您可以使用循环多次运行相同的代码。我们将学习的第一种类型的循环称为“ <code>while</code> ”循环,因为它在“while”运行时指定的条件为true,并且一旦该条件不再为真就停止。 <blockquote> var ourArray = []; <br> var i = 0; <br>而(i <5){ <br> ourArray.push(ⅰ); <br>我++; <br> } </blockquote>让我们尝试通过将值推送到数组来实现while循环。 </section>
|
|
|
|
|
|
|
|
|
|
## Instructions
|
|
|
|
|
<section id="instructions">使用<code>while</code>循环将数字0到4推送到<code>myArray</code> 。 </section>
|
|
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
|
|
```yml
|
|
|
|
|
tests:
|
|
|
|
|
- text: 你应该使用<code>while</code>循环。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(code.match(/while/g));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
- text: '<code>myArray</code>应该等于<code>[0,1,2,3,4]</code> 。'
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert.deepEqual(myArray, [5,4,3,2,1,0]);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
// Setup
|
|
|
|
|
var myArray = [];
|
|
|
|
|
|
|
|
|
|
// 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>
|