61 lines
3.9 KiB
Markdown
61 lines
3.9 KiB
Markdown
---
|
||
id: 587d78b2367417b2b2512b0f
|
||
title: Remove Items from an Array with pop() and shift()
|
||
challengeType: 1
|
||
videoUrl: ''
|
||
localeTitle: Удалите элементы из массива с помощью pop () и shift ()
|
||
---
|
||
|
||
## Description
|
||
<section id="description"> Оба <code>unshift()</code> <code>push()</code> и <code>unshift()</code> имеют соответствующие методы, которые являются почти функциональными противоположностями: <code>pop()</code> и <code>shift()</code> . Как вы уже догадались, вместо добавления <code>pop()</code> <em>удаляет</em> элемент из конца массива, а <code>shift()</code> удаляет элемент с самого начала. Ключевое различие между <code>pop()</code> и <code>shift()</code> и их кузенами <code>push()</code> и <code>unshift()</code> заключается в том, что ни один из методов не принимает параметры, и каждый из них позволяет только массиву изменять один элемент за раз. Давайте взглянем: <blockquote> let greetings = ['whats up?', 'hello', 'see ya!']; <br><br> greetings.pop (); <br> // теперь равно ['whats up?', 'hello'] <br><br> greetings.shift (); <br> // теперь равно ['hello'] </blockquote> Мы также можем вернуть значение удаляемого элемента любым из следующих способов: <blockquote> let popped = greetings.pop (); <br> // возвращает 'hello' <br> // приветствия теперь равны [] </blockquote></section>
|
||
|
||
## Instructions
|
||
<section id="instructions"> Мы определили функцию <code>popShift</code> , которая принимает массив как аргумент и возвращает новый массив. Измените функцию, используя функции <code>pop()</code> и <code>shift()</code> , чтобы удалить первый и последний элементы массива аргументов и присвоить удаленные элементы соответствующим переменным, чтобы возвращаемый массив содержал их значения. </section>
|
||
|
||
## Tests
|
||
<section id='tests'>
|
||
|
||
```yml
|
||
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");'
|
||
|
||
```
|
||
|
||
</section>
|
||
|
||
## Challenge Seed
|
||
<section id='challengeSeed'>
|
||
|
||
<div id='js-seed'>
|
||
|
||
```js
|
||
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']));
|
||
|
||
```
|
||
|
||
</div>
|
||
|
||
|
||
|
||
</section>
|
||
|
||
## Solution
|
||
<section id='solution'>
|
||
|
||
```js
|
||
// solution required
|
||
```
|
||
</section>
|