Array.push()
y Array.unshift()
.
Ambos métodos toman uno o más elementos como parámetros y agregan esos elementos a la matriz en la que se está utilizando el método; el método push()
agrega elementos al final de una matriz, y unshift()
agrega elementos al principio. Considera lo siguiente:
let twentyThree = 'XXIII';
let romanNumerals = ['XXI', 'XXII'];
romanNumerals.unshift('XIX', 'XX');
// now equals ['XIX', 'XX', 'XXI', 'XXII']
romanNumerals.push(twentyThree);
// now equals ['XIX', 'XX', 'XXI', 'XXII', 'XXIII'] Notice that we can also pass variables, which allows us even greater flexibility in dynamically modifying our array's data.
mixedNumbers
, a la que le estamos pasando una matriz como argumento. Modifique la función utilizando push()
y unshift()
para agregar 'I', 2, 'three'
al principio de la matriz y 7, 'VIII', 9
al final para que la matriz devuelta contenga representaciones de los números 1-9 en orden.
mixedNumbers(["IV", 5, "six"])
ahora debe devolver ["I", 2, "three", "IV", 5, "six", 7, "VIII", 9]
'
testString: 'assert.deepEqual(mixedNumbers(["IV", 5, "six"]), ["I", 2, "three", "IV", 5, "six", 7, "VIII", 9], "mixedNumbers(["IV", 5, "six"])
should now return ["I", 2, "three", "IV", 5, "six", 7, "VIII", 9]
");'
- text: La función mixedNumbers
debe utilizar el método push()
testString: 'assert.notStrictEqual(mixedNumbers.toString().search(/\.push\(/), -1, "The mixedNumbers
function should utilize the push()
method");'
- text: La función mixedNumbers
debe utilizar el método unshift()
testString: 'assert.notStrictEqual(mixedNumbers.toString().search(/\.unshift\(/), -1, "The mixedNumbers
function should utilize the unshift()
method");'
```