freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-an.../basic-javascript/iterate-odd-numbers-with-a-...

1.7 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
56104e9e514f539506016a5c Iterate Odd Numbers With a For Loop 1 使用For循环迭代奇数

Description

For循环不必一次迭代一个循环。通过改变我们的final-expression ,我们可以计算偶数。我们将从i = 0开始并在i < 10循环。我们会增加i的2每个回路与i += 2
var ourArray = [];
forvar i = 0; i <10; i + = 2{
ourArray.push;
}
ourArray现在包含[0,2,4,6,8] 。让我们改变initialization这样我们就可以用奇数来计算。

Instructions

使用for循环将奇数从1到9推送到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,3,5,7,9]</code> 。'
    testString: 'assert.deepEqual(myArray, [1,3,5,7,9], "<code>myArray</code> should equal <code>[1,3,5,7,9]</code>.");'

Challenge Seed

// Example
var ourArray = [];

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

// Setup
var myArray = [];

// Only change code below this line.

After Test

console.info('after the test');

Solution

// solution required