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

2.0 KiB

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

--description--

Outra forma de alterar os dados em um array é com a função .pop().

.pop() é usado para remover um valor do final do array. Nós podemos armazenar esse valor removido atribuindo-o a uma variável. Em outras palavras, .pop() remove o último elemento de um array e retorna aquele elemento.

Qualquer tipo de entrada pode ser removida de um array - numbers, strings e até mesmo arrays aninhados.

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

O primeiro console.log exibirá o valor 6 e o segundo exibirá o valor [1, 4].

--instructions--

Use a função .pop() para remover o último item de myArray e atribuir o valor removido para uma nova variável, removedFromMyArray.

--hints--

myArray deve conter apenas [["John", 23]].

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

Você deve usar pop() em myArray.

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

removedFromMyArray deve conter apenas ["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--

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

--seed-contents--

// Setup
const myArray = [["John", 23], ["cat", 2]];

// Only change code below this line

--solutions--

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