2018-10-10 18:03:03 -04:00
---
id: 587d7da9367417b2b2512b67
2021-02-06 04:42:36 +00:00
title: Add Elements to the End of an Array Using concat Instead of push
2018-10-10 18:03:03 -04:00
challengeType: 1
2020-08-05 16:38:04 +08:00
forumTopicId: 301226
2021-01-13 03:31:00 +01:00
dashedName: add-elements-to-the-end-of-an-array-using-concat-instead-of-push
2018-10-10 18:03:03 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2021-02-06 04:42:36 +00:00
Functional programming is all about creating and using non-mutating functions.
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
The last challenge introduced the `concat` method as a way to combine arrays into a new one without mutating the original arrays. Compare `concat` to the `push` method. `Push` adds an item to the end of the same array it is called on, which mutates that array. Here's an example:
2020-08-05 16:38:04 +08:00
```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
```
2021-02-06 04:42:36 +00:00
`Concat` offers a way to add new items to the end of an array without any mutating side effects.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --instructions--
2021-02-06 04:42:36 +00:00
Change the `nonMutatingPush` function so it uses `concat` to add `newItem` to the end of `original` instead of `push` . The function should return an array.
2020-12-16 00:37:30 -07:00
# --hints--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
Your code should use the `concat` method.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(code.match(/\.concat/g));
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
Your code should not use the `push` method.
2018-10-10 18:03:03 -04:00
```js
2021-02-06 04:42:36 +00:00
assert(!code.match(/\.?[\s\S]*?push/g));
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
The `first` array should not change.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(JSON.stringify(first) === JSON.stringify([1, 2, 3]));
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
The `second` array should not change.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(JSON.stringify(second) === JSON.stringify([4, 5]));
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`nonMutatingPush([1, 2, 3], [4, 5])` should return `[1, 2, 3, 4, 5]` .
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(
JSON.stringify(nonMutatingPush([1, 2, 3], [4, 5])) ===
JSON.stringify([1, 2, 3, 4, 5])
);
2018-10-10 18:03:03 -04:00
```
2020-08-05 16:38:04 +08:00
2021-01-13 03:31:00 +01:00
# --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);
```
2020-12-16 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01:00
```js
function nonMutatingPush(original, newItem) {
return original.concat(newItem);
}
var first = [1, 2, 3];
var second = [4, 5];
nonMutatingPush(first, second);
```