* 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.6 KiB
1.6 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
587d7b7b367417b2b2512b17 | 使用展开运算符合并数组 | 1 | 301156 | combine-arrays-with-the-spread-operator |
--description--
展开语法的另一个重要用途是合并数组,或者将某个数组的所有元素插入到另一个数组的任意位置。我们也可以使用 ES5 的语法连接两个数组,但只能让它们首尾相接。而展开语法可以让这样的操作变得极其简单:
let thisArray = ['sage', 'rosemary', 'parsley', 'thyme'];
let thatArray = ['basil', 'cilantro', ...thisArray, 'coriander'];
// thatArray 现在是 ['basil', 'cilantro', 'sage', 'rosemary', 'parsley', 'thyme', 'coriander']
使用展开语法,我们像这样就可以实现一个用传统方法会写得很复杂且冗长的操作。
--instructions--
我们已经定义了一个返回 sentence
变量的 spreadOut
函数。请修改这个函数,利用展开语法使该函数返回数组 ['learning', 'to', 'code', 'is', 'fun']
。
--hints--
spreadOut
应返回 ["learning", "to", "code", "is", "fun"]
。
assert.deepEqual(spreadOut(), ['learning', 'to', 'code', 'is', 'fun']);
spreadOut
函数里应用到展开语法。
assert.notStrictEqual(spreadOut.toString().search(/[...]/), -1);
--seed--
--seed-contents--
function spreadOut() {
let fragment = ['to', 'code'];
let sentence; // Change this line
return sentence;
}
console.log(spreadOut());
--solutions--
function spreadOut() {
let fragment = ['to', 'code'];
let sentence = ['learning', ...fragment, 'is', 'fun'];
return sentence;
}