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

1.8 KiB

id title challengeType videoUrl forumTopicId dashedName
56bbb991ad1ed5201cd392cd Manipula arreglos con shift() 1 https://scrimba.com/c/cRbVETW 18238 manipulate-arrays-with-shift

--description--

pop() siempre elimina el último elemento de un arreglo. ¿Qué tal si quieres eliminar el primero?

Ahí es donde entra .shift(). Funciona igual que .pop(), excepto que elimina el primer elemento en lugar del último.

Ejemplo:

var ourArray = ["Stimpson", "J", ["cat"]];
var removedFromOurArray = ourArray.shift();

removedFromOurArray tendrá una cadena con valor Stimpson y ourArray tendrá ["J", ["cat"]].

--instructions--

Utiliza la función .shift() para eliminar el primer elemento de myArray, asignando el valor "desplazado" a removedFromMyArray.

--hints--

myArray debe ser igual a [["dog", 3]].

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

removedFromMyArray debe contener ["John", 23].

assert(
  (function (d) {
    if (
      d[0] == 'John' &&
      d[1] === 23 &&
      typeof removedFromMyArray === 'object'
    ) {
      return true;
    } else {
      return false;
    }
  })(removedFromMyArray)
);

--seed--

--after-user-code--

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

--seed-contents--

// Setup
var myArray = [["John", 23], ["dog", 3]];

// Only change code below this line
var removedFromMyArray;

--solutions--

var myArray = [["John", 23], ["dog", 3]];

// Only change code below this line
var removedFromMyArray = myArray.shift();