3.9 KiB
3.9 KiB
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(["challenge", "is", "not", "complete"])</code> должен возвращать <code>["challenge", "complete"]</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