2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 587d78b2367417b2b2512b0f
|
|
|
|
challengeType: 1
|
2020-08-04 15:14:21 +08:00
|
|
|
forumTopicId: 301165
|
2020-10-01 17:54:21 +02:00
|
|
|
title: 使用 pop() 和 shift() 从数组中删除项目
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
2020-08-04 15:14:21 +08:00
|
|
|
<section id='description'>
|
|
|
|
<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>的关键区别在于,前者不能接受输入参数,而且每次只能修改数组中的一个元素。
|
|
|
|
让我们来看以下的例子:
|
|
|
|
|
|
|
|
```js
|
|
|
|
let greetings = ['whats up?', 'hello', 'see ya!'];
|
|
|
|
|
|
|
|
greetings.pop();
|
|
|
|
// now equals ['whats up?', 'hello']
|
|
|
|
|
|
|
|
greetings.shift();
|
|
|
|
// now equals ['hello']
|
|
|
|
```
|
|
|
|
|
|
|
|
还可以用这些方法返回移除的元素,像这样:
|
|
|
|
|
|
|
|
```js
|
|
|
|
let popped = greetings.pop();
|
|
|
|
// returns 'hello'
|
|
|
|
// greetings now equals []
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Instructions
|
2020-08-04 15:14:21 +08:00
|
|
|
<section id='instructions'>
|
|
|
|
我们已经定义了一个<code>popShift</code>函数,它会接收一个数组作为输入参数并返回一个新的数组。请你修改这个函数,使用<code>pop()</code>和<code>shift()</code>来移除输入的数组的第一个元素和最后一个元素,并将这两个被移除的元素赋值给对应的变量,使得返回的数组包含它们的值。
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
|
|
|
tests:
|
|
|
|
- text: '<code>popShift(["challenge", "is", "not", "complete"])</code>应返回<code>["challenge", "complete"]</code>'
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(popShift(['challenge', 'is', 'not', 'complete']), ["challenge", "complete"]);
|
2018-10-10 18:03:03 -04:00
|
|
|
- text: <code>popShift</code>函数应该使用<code>pop()</code>方法
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.notStrictEqual(popShift.toString().search(/\.pop\(/), -1);
|
2018-10-10 18:03:03 -04:00
|
|
|
- text: <code>popShift</code>函数应该使用<code>shift()</code>方法
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.notStrictEqual(popShift.toString().search(/\.shift\(/), -1);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</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
|
2020-08-04 15:14:21 +08:00
|
|
|
function popShift(arr) {
|
|
|
|
let popped = arr.pop(); // change this line
|
|
|
|
let shifted = arr.shift(); // change this line
|
|
|
|
return [shifted, popped];
|
|
|
|
}
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
2020-08-04 15:14:21 +08:00
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
</section>
|