Files
freeCodeCamp/curriculum/challenges/ukrainian/02-javascript-algorithms-and-data-structures/basic-javascript/manipulate-arrays-with-pop.md
2023-01-30 18:58:54 +02:00

2.5 KiB
Raw Blame History

id, title, challengeType, videoUrl, forumTopicId, dashedName
id title challengeType videoUrl forumTopicId dashedName
56bbb991ad1ed5201cd392cc Маніпулювання масивами за допомогою методу pop 1 https://scrimba.com/c/cRbVZAB 18236 manipulate-arrays-with-pop

--description--

Змінити дані у масиві також можна за допомогою функції .pop().

.pop() використовують для виведення останнього значення масиву. Ми можемо зберігати виведене значення, присвоюючи його змінній. Іншими словами, .pop() видаляє останній елемент з масиву і повертає цей елемент.

З масиву можна вивести будь-який запис: числа, рядки, навіть вкладені масиви.

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

Перший console.log показуватиме значення 6, а другий показуватиме [1, 4].

--instructions--

Використайте функцію .pop(), щоб вилучити останній елемент з myArray та призначити виведене значення до нової змінної removedFromMyArray.

--hints--

myArray має містити лише [["John", 23]].

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

Ви повинні використати pop() на myArray.

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

removedFromMyArray має містити лише ["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();