1.7 KiB
1.7 KiB
id, challengeType, videoUrl, forumTopicId, title
id | challengeType | videoUrl | forumTopicId | title |
---|---|---|---|---|
56104e9e514f539506016a5c | 1 | https://scrimba.com/c/cm8n7T9 | 18212 | 使用 For 循环遍历数组的奇数 |
Description
计数器
,我们可以按照偶数顺序来迭代。
初始化i = 0
,当i < 10
的时候继续循环。
i += 2
让i
每次循环之后增加2。
var ourArray = [];
for (var i = 0; i < 10; i += 2) {
ourArray.push(i);
}
循环结束后,ourArray
的值为[0,2,4,6,8]
。
改变计数器
,这样我们可以用奇数来数。
Instructions
for
循环,把从 1 到 9 的奇数添加到myArray
。
Tests
tests:
- text: 你应该使用<code>for</code>循环。
testString: assert(code.match(/for\s*\(/g).length > 1);
- text: <code>myArray</code>应该等于<code>[1,3,5,7,9]</code>。
testString: assert.deepEqual(myArray, [1,3,5,7,9]);
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
if(typeof myArray !== "undefined"){(function(){return myArray;})();}
Solution
var ourArray = [];
for (var i = 0; i < 10; i += 2) {
ourArray.push(i);
}
var myArray = [];
for (var i = 1; i < 10; i += 2) {
myArray.push(i);
}