2021-06-15 07:49:18 +00:00
---
id: 587d7da9367417b2b2512b66
2021-07-21 15:23:20 +00:00
title: Combinar dois arrays usando o método concat
2021-06-15 07:49:18 +00:00
challengeType: 1
forumTopicId: 301229
dashedName: combine-two-arrays-using-the-concat-method
---
# --description--
2021-07-26 14:39:21 +00:00
< dfn > Concatenação</ dfn > significa juntar itens de ponta a ponta. Em JavaScript, ambos strings e arrays possuem o método `concat` e ele funciona igualmente para os dois. Para arrays, o método é chamado em uma instância e um segundo array é passado como argumento. `concat` então junta os dois arrays em um só. O método retorna um novo array e deixa os dois originais intactos. Exemplo:
2021-06-15 07:49:18 +00:00
```js
[1, 2, 3].concat([4, 5, 6]);
```
2021-07-16 05:33:16 +00:00
`[1, 2, 3, 4, 5, 6]` é o array retornado.
2021-06-15 07:49:18 +00:00
# --instructions--
2021-07-16 05:33:16 +00:00
Use o método `concat` na função `nonMutatingConcat` para concatenar `attach` ao final de `original` . A função deve retornar o array concatenado.
2021-06-15 07:49:18 +00:00
# --hints--
2021-07-16 05:33:16 +00:00
Você deve usar o método `concat` .
2021-06-15 07:49:18 +00:00
```js
assert(code.match(/\.concat/g));
```
2021-07-16 05:33:16 +00:00
O primeiro array, `first` , não deve ser alterado.
2021-06-15 07:49:18 +00:00
```js
assert(JSON.stringify(first) === JSON.stringify([1, 2, 3]));
```
2021-07-16 05:33:16 +00:00
O segundo array, `second` , não deve ser alterado.
2021-06-15 07:49:18 +00:00
```js
assert(JSON.stringify(second) === JSON.stringify([4, 5]));
```
2021-07-16 05:33:16 +00:00
`nonMutatingConcat([1, 2, 3], [4, 5])` deve retornar `[1, 2, 3, 4, 5]` .
2021-06-15 07:49:18 +00:00
```js
assert(
JSON.stringify(nonMutatingConcat([1, 2, 3], [4, 5])) ===
JSON.stringify([1, 2, 3, 4, 5])
);
```
# --seed--
## --seed-contents--
```js
function nonMutatingConcat(original, attach) {
// Only change code below this line
// Only change code above this line
}
var first = [1, 2, 3];
var second = [4, 5];
nonMutatingConcat(first, second);
```
# --solutions--
```js
function nonMutatingConcat(original, attach) {
// Only change code below this line
return original.concat(attach);
// Only change code above this line
}
var first = [1, 2, 3];
var second = [4, 5];
nonMutatingConcat(first, second);
```