freeCodeCamp/curriculum/challenges/russian/02-javascript-algorithms-an.../basic-data-structures/remove-items-from-an-array-...

3.9 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
587d78b2367417b2b2512b0f Remove Items from an Array with pop() and shift() 1 Удалите элементы из массива с помощью pop () и shift ()

Description

Оба unshift() push() и unshift() имеют соответствующие методы, которые являются почти функциональными противоположностями: pop() и shift() . Как вы уже догадались, вместо добавления pop() удаляет элемент из конца массива, а shift() удаляет элемент с самого начала. Ключевое различие между pop() и shift() и их кузенами push() и unshift() заключается в том, что ни один из методов не принимает параметры, и каждый из них позволяет только массиву изменять один элемент за раз. Давайте взглянем:
let greetings = ['whats up?', 'hello', 'see ya!'];

greetings.pop ();
// теперь равно ['whats up?', 'hello']

greetings.shift ();
// теперь равно ['hello']
Мы также можем вернуть значение удаляемого элемента любым из следующих способов:
let popped = greetings.pop ();
// возвращает 'hello'
// приветствия теперь равны []

Instructions

Мы определили функцию popShift , которая принимает массив как аргумент и возвращает новый массив. Измените функцию, используя функции pop() и shift() , чтобы удалить первый и последний элементы массива аргументов и присвоить удаленные элементы соответствующим переменным, чтобы возвращаемый массив содержал их значения.

Tests

tests:
  - text: '<code>popShift([&quot;challenge&quot;, &quot;is&quot;, &quot;not&quot;, &quot;complete&quot;])</code> должен возвращать <code>[&quot;challenge&quot;, &quot;complete&quot;]</code>'
    testString: 'assert.deepEqual(popShift(["challenge", "is", "not", "complete"]), ["challenge", "complete"], "<code>popShift(["challenge", "is", "not", "complete"])</code> should return <code>["challenge", "complete"]</code>");'
  - text: Функция <code>popShift</code> должна использовать метод <code>pop()</code>
    testString: 'assert.notStrictEqual(popShift.toString().search(/\.pop\(/), -1, "The <code>popShift</code> function should utilize the <code>pop()</code> method");'
  - text: Функция <code>popShift</code> должна использовать метод <code>shift()</code>
    testString: 'assert.notStrictEqual(popShift.toString().search(/\.shift\(/), -1, "The <code>popShift</code> function should utilize the <code>shift()</code> method");'

Challenge Seed

function popShift(arr) {
  let popped; // change this line
  let shifted; // change this line
  return [shifted, popped];
}

// do not change code below this line
console.log(popShift(['challenge', 'is', 'not', 'complete']));

Solution

// solution required