* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
1.4 KiB
1.4 KiB
id, title, challengeType, videoUrl, forumTopicId, dashedName
| id | title | challengeType | videoUrl | forumTopicId | dashedName |
|---|---|---|---|---|---|
| 56105e7b514f539506016a5e | 使用 For 循环反向遍历数组 | 1 | https://scrimba.com/c/c2R6BHa | 16808 | count-backwards-with-a-for-loop |
--description--
for循环也可以逆向迭代,只要我们定义好合适的条件。
为了让每次倒数递减 2,我们需要改变我们的初始化,条件判断和计数器。
我们让i = 10,并且当i > 0的时候才继续循环。我们使用i -= 2来让i每次循环递减 2。
var ourArray = [];
for (var i=10; i > 0; i-=2) {
ourArray.push(i);
}
循环结束后,ourArray的值为[10,8,6,4,2]。 让我们改变初始化和计数器,这样我们就可以按照奇数从后往前两两倒着数。
--instructions--
使用一个for循环,把 9 到 1 的奇数添加进myArray。
--hints--
你应该使用for循环。
assert(code.match(/for\s*\(/g).length > 1);
你应该使用数组方法push。
assert(code.match(/myArray.push/));
myArray应该等于[9,7,5,3,1]。
assert.deepEqual(myArray, [9, 7, 5, 3, 1]);
--seed--
--after-user-code--
if(typeof myArray !== "undefined"){(function(){return myArray;})();}
--seed-contents--
// Setup
var myArray = [];
// Only change code below this line
--solutions--
var myArray = [];
for (var i = 9; i > 0; i -= 2) {
myArray.push(i);
}