91 lines
2.0 KiB
Markdown
91 lines
2.0 KiB
Markdown
---
|
|
id: 56bbb991ad1ed5201cd392cc
|
|
title: Manipula arreglos con pop()
|
|
challengeType: 1
|
|
videoUrl: 'https://scrimba.com/c/cRbVZAB'
|
|
forumTopicId: 18236
|
|
dashedName: manipulate-arrays-with-pop
|
|
---
|
|
|
|
# --description--
|
|
|
|
Otra manera de cambiar los datos en un arreglo es con la función `.pop()`.
|
|
|
|
`.pop()` se utiliza para sacar un valor del final de un arreglo. Podemos almacenar este valor sacado asignándolo a una variable. En otras palabras, `.pop()` elimina el último elemento de un arreglo y devuelve ese elemento.
|
|
|
|
Cualquier tipo de entrada puede ser sacada de un arreglo: números, cadenas, incluso arreglos anidados.
|
|
|
|
```js
|
|
const threeArr = [1, 4, 6];
|
|
const oneDown = threeArr.pop();
|
|
console.log(oneDown);
|
|
console.log(threeArr);
|
|
```
|
|
|
|
El primer `console.log` mostrará el valor `6` y el segundo mostrará el valor `[1, 4]`.
|
|
|
|
# --instructions--
|
|
|
|
Utiliza la función `.pop()` para eliminar el último elemento de `myArray`, y asigna el valor sacado a un variable nuevo `removedFromMyArray`.
|
|
|
|
# --hints--
|
|
|
|
`myArray` sólo debe contener `[["John", 23]]`.
|
|
|
|
```js
|
|
assert(
|
|
(function (d) {
|
|
if (d[0][0] == 'John' && d[0][1] === 23 && d[1] == undefined) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
})(myArray)
|
|
);
|
|
```
|
|
|
|
Debes usar `pop()` en `myArray`.
|
|
|
|
```js
|
|
assert(/removedFromMyArray\s*=\s*myArray\s*.\s*pop\s*(\s*)/.test(code));
|
|
```
|
|
|
|
`removedFromMyArray` sólo debe contener `["cat", 2]`.
|
|
|
|
```js
|
|
assert(
|
|
(function (d) {
|
|
if (d[0] == 'cat' && d[1] === 2 && d[2] == undefined) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
})(removedFromMyArray)
|
|
);
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --after-user-code--
|
|
|
|
```js
|
|
if (typeof removedFromMyArray !== 'undefined') (function(y, z){return 'myArray = ' + JSON.stringify(y) + ' & removedFromMyArray = ' + JSON.stringify(z);})(myArray, removedFromMyArray);
|
|
```
|
|
|
|
## --seed-contents--
|
|
|
|
```js
|
|
// Setup
|
|
const myArray = [["John", 23], ["cat", 2]];
|
|
|
|
// Only change code below this line
|
|
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```js
|
|
const myArray = [["John", 23], ["cat", 2]];
|
|
const removedFromMyArray = myArray.pop();
|
|
```
|