* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
83 lines
2.3 KiB
Markdown
83 lines
2.3 KiB
Markdown
---
|
|
id: 587d78b2367417b2b2512b0f
|
|
title: Remove Items from an Array with pop() and shift()
|
|
challengeType: 1
|
|
forumTopicId: 301165
|
|
dashedName: remove-items-from-an-array-with-pop-and-shift
|
|
---
|
|
|
|
# --description--
|
|
|
|
Both `push()` and `unshift()` have corresponding methods that are nearly functional opposites: `pop()` and `shift()`. As you may have guessed by now, instead of adding, `pop()` *removes* an element from the end of an array, while `shift()` removes an element from the beginning. The key difference between `pop()` and `shift()` and their cousins `push()` and `unshift()`, is that neither method takes parameters, and each only allows an array to be modified by a single element at a time.
|
|
|
|
Let's take a look:
|
|
|
|
```js
|
|
let greetings = ['whats up?', 'hello', 'see ya!'];
|
|
|
|
greetings.pop();
|
|
// now equals ['whats up?', 'hello']
|
|
|
|
greetings.shift();
|
|
// now equals ['hello']
|
|
```
|
|
|
|
We can also return the value of the removed element with either method like this:
|
|
|
|
```js
|
|
let popped = greetings.pop();
|
|
// returns 'hello'
|
|
// greetings now equals []
|
|
```
|
|
|
|
# --instructions--
|
|
|
|
We have defined a function, `popShift`, which takes an array as an argument and returns a new array. Modify the function, using `pop()` and `shift()`, to remove the first and last elements of the argument array, and assign the removed elements to their corresponding variables, so that the returned array contains their values.
|
|
|
|
# --hints--
|
|
|
|
`popShift(["challenge", "is", "not", "complete"])` should return `["challenge", "complete"]`
|
|
|
|
```js
|
|
assert.deepEqual(popShift(['challenge', 'is', 'not', 'complete']), [
|
|
'challenge',
|
|
'complete'
|
|
]);
|
|
```
|
|
|
|
The `popShift` function should utilize the `pop()` method
|
|
|
|
```js
|
|
assert.notStrictEqual(popShift.toString().search(/\.pop\(/), -1);
|
|
```
|
|
|
|
The `popShift` function should utilize the `shift()` method
|
|
|
|
```js
|
|
assert.notStrictEqual(popShift.toString().search(/\.shift\(/), -1);
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```js
|
|
function popShift(arr) {
|
|
let popped; // Change this line
|
|
let shifted; // Change this line
|
|
return [shifted, popped];
|
|
}
|
|
|
|
console.log(popShift(['challenge', 'is', 'not', 'complete']));
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```js
|
|
function popShift(arr) {
|
|
let popped = arr.pop(); // Change this line
|
|
let shifted = arr.shift(); // Change this line
|
|
return [shifted, popped];
|
|
}
|
|
```
|