2021-02-06 04:42:36 +00:00
---
id: 587d7da9367417b2b2512b67
2021-03-17 13:48:12 -06:00
title: Agrega elementos al final de un arreglo utilizando concat en lugar de push
2021-02-06 04:42:36 +00:00
challengeType: 1
forumTopicId: 301226
dashedName: add-elements-to-the-end-of-an-array-using-concat-instead-of-push
---
# --description--
2021-03-17 13:48:12 -06:00
La programación funcional consiste en crear y utilizar funciones no mutantes.
2021-02-06 04:42:36 +00:00
2021-03-17 13:48:12 -06:00
El último desafío introdujo el método `concat` como una forma de combinar arreglos en uno nuevo sin mutar los arreglos originales. Compara `concat` con el método `push` . `push` añade un elemento al final del arreglo desde el que se llama, lo cual muta ese arreglo. Aquí hay un ejemplo:
2021-02-06 04:42:36 +00:00
```js
2021-10-27 15:10:57 +00:00
const arr = [1, 2, 3];
2021-02-06 04:42:36 +00:00
arr.push([4, 5, 6]);
```
2021-03-17 13:48:12 -06:00
`arr` tendría un valor modificado de `[1, 2, 3, [4, 5, 6]]` , que no encaja con el paradigma de la programación funcional.
`concat` ofrece una forma de añadir nuevos elementos al final de un arreglo, sin provocar ningún efecto de mutación.
2021-02-06 04:42:36 +00:00
# --instructions--
2021-03-17 13:48:12 -06:00
Cambia la función `nonMutatingPush` para que use `concat` para añadir `newItem` al final de `original` en lugar de `push` . La función debe devolver un arreglo.
2021-02-06 04:42:36 +00:00
# --hints--
2021-03-17 13:48:12 -06:00
El código debe utilizar el método `concat` .
2021-02-06 04:42:36 +00:00
```js
assert(code.match(/\.concat/g));
```
2021-03-17 13:48:12 -06:00
El código no debe utilizar el método `push` .
2021-02-06 04:42:36 +00:00
```js
assert(!code.match(/\.?[\s\S]*?push/g));
```
2021-03-17 13:48:12 -06:00
El arreglo `first` no debe modificarse.
2021-02-06 04:42:36 +00:00
```js
assert(JSON.stringify(first) === JSON.stringify([1, 2, 3]));
```
2021-03-17 13:48:12 -06:00
El arreglo `second` no debe modificarse.
2021-02-06 04:42:36 +00:00
```js
assert(JSON.stringify(second) === JSON.stringify([4, 5]));
```
2021-03-17 13:48:12 -06:00
`nonMutatingPush([1, 2, 3], [4, 5])` debe devolver `[1, 2, 3, 4, 5]` .
2021-02-06 04:42:36 +00:00
```js
assert(
JSON.stringify(nonMutatingPush([1, 2, 3], [4, 5])) ===
JSON.stringify([1, 2, 3, 4, 5])
);
```
# --seed--
## --seed-contents--
```js
function nonMutatingPush(original, newItem) {
// Only change code below this line
return original.push(newItem);
// Only change code above this line
}
2021-10-27 15:10:57 +00:00
const first = [1, 2, 3];
const second = [4, 5];
2021-02-06 04:42:36 +00:00
nonMutatingPush(first, second);
```
# --solutions--
```js
function nonMutatingPush(original, newItem) {
return original.concat(newItem);
}
2021-10-27 15:10:57 +00:00
const first = [1, 2, 3];
const second = [4, 5];
2021-02-06 04:42:36 +00:00
```