push()
como unshift()
tienen métodos correspondientes que son opuestos casi funcionales: pop()
y shift()
. Como ya habrá adivinado, en lugar de agregar, pop()
elimina un elemento del final de una matriz, mientras que shift()
elimina un elemento desde el principio. La diferencia clave entre pop()
y shift()
y sus primos push()
y unshift()
, es que ninguno de los dos métodos toma parámetros, y cada uno solo permite que una matriz sea modificada por un solo elemento a la vez.
Echemos un vistazo:
let greetings = ['whats up?', 'hello', 'see ya!'];También podemos devolver el valor del elemento eliminado con uno de estos métodos:
greetings.pop();
// now equals ['whats up?', 'hello']
greetings.shift();
// now equals ['hello']
let popped = greetings.pop();
// returns 'hello'
// greetings now equals []
popShift
, que toma una matriz como argumento y devuelve una nueva matriz. Modifique la función, utilizando pop()
y shift()
, para eliminar el primer y último elemento de la matriz de argumentos, y asigne los elementos eliminados a sus variables correspondientes, de modo que la matriz devuelta contenga sus valores.
popShift(["challenge", "is", "not", "complete"])
debe devolver ["challenge", "complete"]
'
testString: 'assert.deepEqual(popShift(["challenge", "is", "not", "complete"]), ["challenge", "complete"], "popShift(["challenge", "is", "not", "complete"])
should return ["challenge", "complete"]
");'
- text: La función popShift
debería utilizar el método pop()
testString: 'assert.notStrictEqual(popShift.toString().search(/\.pop\(/), -1, "The popShift
function should utilize the pop()
method");'
- text: La función popShift
debería utilizar el método shift()
testString: 'assert.notStrictEqual(popShift.toString().search(/\.shift\(/), -1, "The popShift
function should utilize the shift()
method");'
```