2018-09-30 23:01:58 +01:00
---
id: 587d7da9367417b2b2512b67
title: Add Elements to the End of an Array Using concat Instead of push
challengeType: 1
2020-05-21 17:31:25 +02:00
isHidden: false
2019-08-05 09:17:33 -07:00
forumTopicId: 301226
2018-09-30 23:01:58 +01:00
---
## Description
< section id = 'description' >
Functional programming is all about creating and using non-mutating functions.
The last challenge introduced the < code > concat< / code > method as a way to combine arrays into a new one without mutating the original arrays. Compare < code > concat< / code > to the < code > push< / code > method. < code > Push< / code > adds an item to the end of the same array it is called on, which mutates that array. Here's an example:
2019-05-17 06:20:30 -07: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
```
2018-09-30 23:01:58 +01:00
< code > Concat< / code > offers a way to add new items to the end of an array without any mutating side effects.
< / section >
## Instructions
< section id = 'instructions' >
Change the < code > nonMutatingPush< / code > function so it uses < code > concat< / code > to add < code > newItem< / code > to the end of < code > original< / code > instead of < code > push< / code > . The function should return an array.
< / section >
## Tests
< section id = 'tests' >
```yml
2018-10-04 14:37:37 +01:00
tests:
- text: Your code should use the < code > concat</ code > method.
2019-07-24 01:47:32 -07:00
testString: assert(code.match(/\.concat/g));
2018-10-04 14:37:37 +01:00
- text: Your code should not use the < code > push</ code > method.
2020-03-26 20:13:34 +05:00
testString: assert(!code.match(/\.?[\s\S]*?push/g));
2018-10-04 14:37:37 +01:00
- text: The < code > first</ code > array should not change.
2019-07-24 01:47:32 -07:00
testString: assert(JSON.stringify(first) === JSON.stringify([1, 2, 3]));
2018-10-04 14:37:37 +01:00
- text: The < code > second</ code > array should not change.
2019-07-24 01:47:32 -07:00
testString: assert(JSON.stringify(second) === JSON.stringify([4, 5]));
2018-10-20 21:02:47 +03:00
- text: < code > nonMutatingPush([1, 2, 3], [4, 5])</ code > should return < code > [1, 2, 3, 4, 5]</ code > .
2019-07-24 01:47:32 -07:00
testString: assert(JSON.stringify(nonMutatingPush([1, 2, 3], [4, 5])) === JSON.stringify([1, 2, 3, 4, 5]));
2018-09-30 23:01:58 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
function nonMutatingPush(original, newItem) {
2020-03-08 07:46:28 -07:00
// Only change code below this line
2018-09-30 23:01:58 +01:00
return original.push(newItem);
2018-10-08 01:01:53 +01:00
2020-03-08 07:46:28 -07:00
// Only change code above this line
2018-09-30 23:01:58 +01:00
}
var first = [1, 2, 3];
var second = [4, 5];
nonMutatingPush(first, second);
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
2019-04-28 02:01:14 -07:00
function nonMutatingPush(original, newItem) {
return original.concat(newItem);
}
var first = [1, 2, 3];
var second = [4, 5];
nonMutatingPush(first, second);
2018-09-30 23:01:58 +01:00
```
2019-07-18 08:24:12 -07:00
2018-09-30 23:01:58 +01:00
< / section >