* 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>
89 lines
1.9 KiB
Markdown
89 lines
1.9 KiB
Markdown
---
|
|
id: 587d7da9367417b2b2512b67
|
|
title: 使用 concat 而不是 push 将元素添加到数组的末尾
|
|
challengeType: 1
|
|
forumTopicId: 301226
|
|
dashedName: add-elements-to-the-end-of-an-array-using-concat-instead-of-push
|
|
---
|
|
|
|
# --description--
|
|
|
|
函数式编程就是创建和使用具有不变性的函数。
|
|
|
|
上一个挑战介绍了`concat`方法,这是一种在不改变原始数组的前提下,将数组组合成新数组的方法。将`concat`方法与`push`方法做比较,`Push`将元素添加到调用它的数组的末尾,这样会改变该数组。举个例子:
|
|
|
|
```js
|
|
var arr = [1, 2, 3];
|
|
arr.push([4, 5, 6]);
|
|
// arr is changed to [1, 2, 3, [4, 5, 6]]
|
|
// Not the functional programming way
|
|
```
|
|
|
|
`Concat`方法可以将新项目添加到数组末尾,而不产生任何变化。
|
|
|
|
# --instructions--
|
|
|
|
修改`nonMutatingPush`函数,用`concat`替代`push`将`newItem`添加到`original`末尾,该函数应返回一个数组。
|
|
|
|
# --hints--
|
|
|
|
应该使用`concat`方法。
|
|
|
|
```js
|
|
assert(code.match(/\.concat/g));
|
|
```
|
|
|
|
不能使用`push`方法。
|
|
|
|
```js
|
|
assert(!code.match(/\.push/g));
|
|
```
|
|
|
|
不能改变`first`数组。
|
|
|
|
```js
|
|
assert(JSON.stringify(first) === JSON.stringify([1, 2, 3]));
|
|
```
|
|
|
|
不能改变`second`数组。
|
|
|
|
```js
|
|
assert(JSON.stringify(second) === JSON.stringify([4, 5]));
|
|
```
|
|
|
|
`nonMutatingPush([1, 2, 3], [4, 5])`应返回`[1, 2, 3, 4, 5]`。
|
|
|
|
```js
|
|
assert(
|
|
JSON.stringify(nonMutatingPush([1, 2, 3], [4, 5])) ===
|
|
JSON.stringify([1, 2, 3, 4, 5])
|
|
);
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```js
|
|
function nonMutatingPush(original, newItem) {
|
|
// Only change code below this line
|
|
return original.push(newItem);
|
|
|
|
// Only change code above this line
|
|
}
|
|
var first = [1, 2, 3];
|
|
var second = [4, 5];
|
|
nonMutatingPush(first, second);
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```js
|
|
function nonMutatingPush(original, newItem) {
|
|
return original.concat(newItem);
|
|
}
|
|
var first = [1, 2, 3];
|
|
var second = [4, 5];
|
|
nonMutatingPush(first, second);
|
|
```
|