1.6 KiB

id, title, challengeType, videoUrl, forumTopicId, localeTitle
id title challengeType videoUrl forumTopicId localeTitle
cf1111c1c11feddfaeb1bdef Iterate with JavaScript While Loops 1 https://scrimba.com/c/c8QbnCM 18220 while 循环

Description

你可以使用循环多次执行相同的代码。 我们将学习的第一种类型的循环称为 "while" 循环,因为它规定,当 "while" 条件为真,循环才会执行,反之不执行。
var ourArray = [];
var i = 0;
while(i < 5) {
  ourArray.push(i);
  i++;
}

在上面的代码里,while 循环执行 5 次把 0 到 4 的数字添加到 ourArray 数组里。

让我们通过 while 循环将值添加到数组中。

Instructions

通过一个while循环,把从 0 到 4 的值添加到myArray中。

Tests

tests:
  - text: 你应该使用<code>while</code>循环。
    testString: assert(code.match(/while/g));
  - text: <code>myArray</code>应该等于<code>[0,1,2,3,4]</code>。
    testString: assert.deepEqual(myArray, [0,1,2,3,4]);

Challenge Seed

// Setup
var myArray = [];

// Only change code below this line.


After Test

if(typeof myArray !== "undefined"){(function(){return myArray;})();}

Solution

var myArray = [];
var i = 5;
while(i >= 0) {
  myArray.push(i);
  i--;
}