freeCodeCamp/curriculum/challenges/italian/02-javascript-algorithms-an.../basic-javascript/manipulate-arrays-with-pop.md

2.0 KiB

id title challengeType videoUrl forumTopicId dashedName
56bbb991ad1ed5201cd392cc Manipolare gli array con pop() 1 https://scrimba.com/c/cRbVZAB 18236 manipulate-arrays-with-pop

--description--

Un altro modo per cambiare i dati in un array è con la funzione .pop().

.pop() è usato per estrarre un valore dalla fine di un array. Possiamo memorizzare questo valore assegnandolo ad una variabile. In altre parole, .pop() rimuove l'ultimo elemento da un array e restituisce quell'elemento.

Qualsiasi tipo di elemento può essere estratto da un array - numeri, stringhe, anche array annidati.

var threeArr = [1, 4, 6];
var oneDown = threeArr.pop();
console.log(oneDown);
console.log(threeArr);

Il primo console.log mostrerà il valore 6e il secondo mostrerà il valore [1, 4].

--instructions--

Utilizza la funzione .pop() per rimuovere l'ultimo elemento da myArray, assegnando il valore estratto a removedFromMyArray.

--hints--

myArray dovrebbe contenere solo [["John", 23]].

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

Dovresti usare pop() su myArray.

assert(/removedFromMyArray\s*=\s*myArray\s*.\s*pop\s*(\s*)/.test(code));

removedFromMyArray dovrebbe contenere solo ["cat", 2].

assert(
  (function (d) {
    if (d[0] == 'cat' && d[1] === 2 && d[2] == undefined) {
      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], ["cat", 2]];

// Only change code below this line
var removedFromMyArray;

--solutions--

var myArray = [["John", 23], ["cat", 2]];
var removedFromMyArray = myArray.pop();