.github
api-server
client
config
curriculum
__fixtures__
challenges
_meta
arabic
chinese
english
01-responsive-web-design
02-javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
access-an-arrays-contents-using-bracket-notation.english.md
access-property-names-with-bracket-notation.english.md
add-items-to-an-array-with-push-and-unshift.english.md
add-items-using-splice.english.md
add-key-value-pairs-to-javascript-objects.english.md
check-for-the-presence-of-an-element-with-indexof.english.md
check-if-an-object-has-a-property.english.md
combine-arrays-with-the-spread-operator.english.md
copy-an-array-with-the-spread-operator.english.md
copy-array-items-using-slice.english.md
create-complex-multi-dimensional-arrays.english.md
generate-an-array-of-all-object-keys-with-object.keys.english.md
iterate-through-all-an-arrays-items-using-for-loops.english.md
iterate-through-the-keys-of-an-object-with-a-for...in-statement.english.md
modify-an-array-stored-in-an-object.english.md
modify-an-object-nested-within-an-object.english.md
remove-items-from-an-array-with-pop-and-shift.english.md
remove-items-using-splice.english.md
use-an-array-to-store-a-collection-of-data.english.md
use-the-delete-keyword-to-remove-object-properties.english.md
basic-javascript
debugging
es6
functional-programming
intermediate-algorithm-scripting
javascript-algorithms-and-data-structures-projects
object-oriented-programming
regular-expressions
03-front-end-libraries
04-data-visualization
05-apis-and-microservices
06-quality-assurance
07-scientific-computing-with-python
08-data-analysis-with-python
09-information-security
10-coding-interview-prep
11-machine-learning-with-python
12-certificates
portuguese
russian
spanish
schema
test
.babelrc
.editorconfig
.npmignore
.travis.yml
CHANGELOG.md
LICENSE.md
comment-dictionary.js
commitizen.config.js
commitlint.config.js
create-challenge-bundle.js
getChallenges.acceptance.test.js
getChallenges.js
getChallenges.test.js
gulpfile.js
lib.js
package-entry.js
package-lock.json
package.json
utils.js
cypress
docs
tools
utils
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.gitpod.yml
.node-inspectorrc
.npmrc
.prettierignore
.prettierrc
.snyk
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile.tests
HoF.md
LICENSE.md
README.md
SECURITY.md
change_volumes_owner.sh
crowdin.yml
cypress-install.js
cypress.json
docker-compose-shared.yml
docker-compose.tests.yml
docker-compose.yml
jest.config.js
lerna.json
lighthouserc.js
package-lock.json
package.json
sample.env
This includes certificates (where it does nothing), but does not include any translations.
88 lines
2.7 KiB
Markdown
88 lines
2.7 KiB
Markdown
---
|
|
id: 587d78b2367417b2b2512b0f
|
|
title: Remove Items from an Array with pop() and shift()
|
|
challengeType: 1
|
|
isHidden: false
|
|
forumTopicId: 301165
|
|
---
|
|
|
|
## Description
|
|
<section id='description'>
|
|
Both <code>push()</code> and <code>unshift()</code> have corresponding methods that are nearly functional opposites: <code>pop()</code> and <code>shift()</code>. As you may have guessed by now, instead of adding, <code>pop()</code> <em>removes</em> an element from the end of an array, while <code>shift()</code> removes an element from the beginning. The key difference between <code>pop()</code> and <code>shift()</code> and their cousins <code>push()</code> and <code>unshift()</code>, 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 []
|
|
```
|
|
|
|
</section>
|
|
|
|
## Instructions
|
|
<section id='instructions'>
|
|
We have defined a function, <code>popShift</code>, which takes an array as an argument and returns a new array. Modify the function, using <code>pop()</code> and <code>shift()</code>, 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.
|
|
</section>
|
|
|
|
## Tests
|
|
<section id='tests'>
|
|
|
|
```yml
|
|
tests:
|
|
- text: <code>popShift(["challenge", "is", "not", "complete"])</code> should return <code>["challenge", "complete"]</code>
|
|
testString: assert.deepEqual(popShift(['challenge', 'is', 'not', 'complete']), ["challenge", "complete"]);
|
|
- text: The <code>popShift</code> function should utilize the <code>pop()</code> method
|
|
testString: assert.notStrictEqual(popShift.toString().search(/\.pop\(/), -1);
|
|
- text: The <code>popShift</code> function should utilize the <code>shift()</code> method
|
|
testString: assert.notStrictEqual(popShift.toString().search(/\.shift\(/), -1);
|
|
|
|
```
|
|
|
|
</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];
|
|
}
|
|
|
|
console.log(popShift(['challenge', 'is', 'not', 'complete']));
|
|
```
|
|
|
|
</div>
|
|
|
|
|
|
|
|
</section>
|
|
|
|
## Solution
|
|
<section id='solution'>
|
|
|
|
```js
|
|
function popShift(arr) {
|
|
let popped = arr.pop(); // Change this line
|
|
let shifted = arr.shift(); // Change this line
|
|
return [shifted, popped];
|
|
}
|
|
```
|
|
|
|
</section>
|