zilongxuan001 / LearnFreecode

0 stars 0 forks source link

Iterate Odd Numbers With a For Loop #261

Open zilongxuan001 opened 6 years ago

zilongxuan001 commented 6 years ago

介绍

For loops不仅能一次遍历一个数,改变fianl-expression,我们可以计算偶数。

方法

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

ourArray will now contain [0,2,4,6,8].

练习

Push the odd numbers from 1 through 9 to myArray using a for loop.

代码


// Example
var ourArray = [];

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

// Setup
var myArray = [];

// Only change code below this line.

for (var i = 1; i < 10; i +=2) {
  myArray.push(i);
}

结果显示

image

来源

https://www.freecodecamp.org/challenges/iterate-odd-numbers-with-a-for-loop