2.5 KiB
Raw Blame History

id, title, challengeType, videoUrl, localeTitle
id title challengeType videoUrl localeTitle
cf1111c1c11feddfaeb5bdef Iterate with JavaScript For Loops 1 使用JavaScript迭代循环

Description

您可以使用循环多次运行相同的代码。最常见的JavaScript循环类型称为“ for loop ”,因为它“运行”特定次数。 For循环用三个可选表达式声明用分号分隔 for ([initialization]; [condition]; [final-expression]) initialization语句仅在循环开始之前执行一次。它通常用于定义和设置循环变量。 condition语句在每次循环迭代开始时进行计算,并且只要计算结果为true就会继续。当迭代开始时conditionfalse时,循环将停止执行。这意味着如果conditionfalse开头,则循环将永远不会执行。 final-expression在每次循环迭代结束时执行,在下一次condition检查之前执行,通常用于递增或递减循环计数器。在下面的示例中,我们使用i = 0初始化并迭代,而条件i < 5为真。我们将在每个循环迭代中将i递增1 ,并使用i++作为final-expression
var ourArray = [];
forvar i = 0; i <5; i ++{
ourArray.push;
}
ourArray现在包含[0,1,2,3,4]

Instructions

使用for循环将值1到5推送到myArray

Tests

tests:
  - text: 你应该为此使用<code>for</code>循环。
    testString: 'assert(code.match(/for\s*\(/g).length > 1, "You should be using a <code>for</code> loop for this.");'
  - text: '<code>myArray</code>应该等于<code>[1,2,3,4,5]</code> 。'
    testString: 'assert.deepEqual(myArray, [1,2,3,4,5], "<code>myArray</code> should equal <code>[1,2,3,4,5]</code>.");'

Challenge Seed

// Example
var ourArray = [];

for (var i = 0; i < 5; i++) {
  ourArray.push(i);
}

// Setup
var myArray = [];

// Only change code below this line.

After Test

console.info('after the test');

Solution

// solution required