freeCodeCamp/curriculum/challenges/espanol/02-javascript-algorithms-an.../basic-javascript/manipulate-arrays-with-unsh...

1.7 KiB

id title challengeType videoUrl forumTopicId dashedName
56bbb991ad1ed5201cd392ce Manipula arreglos con unshift() 1 https://scrimba.com/c/ckNDESv 18239 manipulate-arrays-with-unshift

--description--

No solo puedes desplazar (shift) elementos del comienzo de un arreglo, también puedes des-desplazar (unshift) elementos al comienzo de un arreglo. Por ejemplo añadir elementos delante del arreglo.

.unshift() funciona exactamente como .push(), pero en lugar de añadir el elemento al final del arreglo, unshift() añade el elemento al principio del arreglo.

Ejemplo:

const ourArray = ["Stimpson", "J", "cat"];
ourArray.shift();
ourArray.unshift("Happy");

Después del shift, ourArray tendrá el valor ["J", "cat"]. Después del unshift, ourArray tendrá el valor ["Happy", "J", "cat"].

--instructions--

Agrega ["Paul", 35] al principio de la variable myArray usando unshift().

--hints--

myArray debe contener ahora [["Paul", 35], ["dog", 3]].

assert(
  (function (d) {
    if (
      typeof d[0] === 'object' &&
      d[0][0] == 'Paul' &&
      d[0][1] === 35 &&
      d[1][0] != undefined &&
      d[1][0] == 'dog' &&
      d[1][1] != undefined &&
      d[1][1] == 3
    ) {
      return true;
    } else {
      return false;
    }
  })(myArray)
);

--seed--

--after-user-code--

(function(y, z){return 'myArray = ' + JSON.stringify(y);})(myArray);

--seed-contents--

// Setup
const myArray = [["John", 23], ["dog", 3]];
myArray.shift();

// Only change code below this line

--solutions--

const myArray = [["John", 23], ["dog", 3]];
myArray.shift();
myArray.unshift(["Paul", 35]);