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
2021-01-13 03:31:00 +01:00
dashedName: combine-two-arrays-using-the-concat-method
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
< dfn > Concatenation</ dfn > means to join items end to end. JavaScript offers the `concat` 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 `concat` , 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]
```
2020-11-27 19:02:05 +01:00
# --instructions--
Use the `concat` method in the `nonMutatingConcat` function to concatenate `attach` to the end of `original` . The function should return the concatenated array.
# --hints--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
Your code should use the `concat` method.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(code.match(/\.concat/g));
```
The `first` array should not change.
```js
assert(JSON.stringify(first) === JSON.stringify([1, 2, 3]));
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
The `second` array should not change.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(JSON.stringify(second) === JSON.stringify([4, 5]));
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
`nonMutatingConcat([1, 2, 3], [4, 5])` should return `[1, 2, 3, 4, 5]` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(
JSON.stringify(nonMutatingConcat([1, 2, 3], [4, 5])) ===
JSON.stringify([1, 2, 3, 4, 5])
);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --seed--
## --seed-contents--
2018-09-30 23:01:58 +01:00
```js
function nonMutatingConcat(original, attach) {
2020-03-08 07:46:28 -07:00
// Only change code below this line
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];
nonMutatingConcat(first, second);
```
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
2019-04-28 02:01:14 -07:00
function nonMutatingConcat(original, attach) {
2020-03-08 07:46:28 -07:00
// Only change code below this line
2019-04-28 02:01:14 -07:00
return original.concat(attach);
2020-03-08 07:46:28 -07:00
// Only change code above this line
2019-04-28 02:01:14 -07:00
}
var first = [1, 2, 3];
var second = [4, 5];
nonMutatingConcat(first, second);
2018-09-30 23:01:58 +01:00
```