2018-09-30 23:01:58 +01:00
---
id: 587d7da9367417b2b2512b66
title: Combine Two Arrays Using the concat Method
challengeType: 1
2019-08-05 09:17:33 -07:00
forumTopicId: 301229
2018-09-30 23:01:58 +01:00
---
## Description
< section id = 'description' >
2019-10-27 15:45:37 -01:00
< dfn > Concatenation< / dfn > means to join items end to end. JavaScript offers the < code > concat< / code > method for both strings and arrays that work in the same way. For arrays, the method is called on one, then another array is provided as the argument to < code > concat< / code > , which is added to the end of the first array. It returns a new array and does not mutate either of the original arrays. Here's an example:
2019-05-17 06:20:30 -07:00
```js
[1, 2, 3].concat([4, 5, 6]);
// Returns a new array [1, 2, 3, 4, 5, 6]
```
2018-09-30 23:01:58 +01:00
< / section >
## Instructions
< section id = 'instructions' >
Use the < code > concat< / code > method in the < code > nonMutatingConcat< / code > function to concatenate < code > attach< / code > to the end of < code > original< / code > . The function should return the concatenated 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: 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 > nonMutatingConcat([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(nonMutatingConcat([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 nonMutatingConcat(original, attach) {
// Add your code below this line
2018-10-08 01:01:53 +01:00
2018-09-30 23:01:58 +01:00
// Add your code above this line
}
var first = [1, 2, 3];
var second = [4, 5];
nonMutatingConcat(first, second);
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
2019-04-28 02:01:14 -07:00
function nonMutatingConcat(original, attach) {
// Add your code below this line
return original.concat(attach);
// Add your code above this line
}
var first = [1, 2, 3];
var second = [4, 5];
nonMutatingConcat(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 >