2018-10-08 13:34:43 -04:00

3.2 KiB

id, title, localeTitle, challengeType
id title localeTitle challengeType
587d78b2367417b2b2512b0e Add Items to an Array with push() and unshift() Agregar elementos a una matriz con push () y unshift () 1

Description

La longitud de una matriz, como los tipos de datos que puede contener, no es fija. Las matrices se pueden definir con una longitud de cualquier número de elementos, y los elementos se pueden agregar o eliminar con el tiempo; en otras palabras, los arreglos son mutables . En este desafío, veremos dos métodos con los cuales podemos modificar mediante programación una matriz: 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.

Instructions

Hemos definido una función, 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.

Tests

tests:
  - text: &#39; <code>mixedNumbers([&quot;IV&quot;, 5, &quot;six&quot;])</code> ahora debe devolver <code>[&quot;I&quot;, 2, &quot;three&quot;, &quot;IV&quot;, 5, &quot;six&quot;, 7, &quot;VIII&quot;, 9]</code> &#39;
    testString: 'assert.deepEqual(mixedNumbers(["IV", 5, "six"]), ["I", 2, "three", "IV", 5, "six", 7, "VIII", 9], "<code>mixedNumbers(["IV", 5, "six"])</code> should now return <code>["I", 2, "three", "IV", 5, "six", 7, "VIII", 9]</code>");'
  - text: La función <code>mixedNumbers</code> debe utilizar el método <code>push()</code>
    testString: 'assert.notStrictEqual(mixedNumbers.toString().search(/\.push\(/), -1, "The <code>mixedNumbers</code> function should utilize the <code>push()</code> method");'
  - text: La función <code>mixedNumbers</code> debe utilizar el método <code>unshift()</code>
    testString: 'assert.notStrictEqual(mixedNumbers.toString().search(/\.unshift\(/), -1, "The <code>mixedNumbers</code> function should utilize the <code>unshift()</code> method");'

Challenge Seed

function mixedNumbers(arr) {
  // change code below this line

  // change code above this line
  return arr;
}

// do not change code below this line
console.log(mixedNumbers(['IV', 5, 'six']));

Solution

// solution required