2018-09-30 23:01:58 +01:00
---
id: 56bbb991ad1ed5201cd392cc
title: Manipulate Arrays With pop()
challengeType: 1
2019-02-14 12:24:02 -05:00
videoUrl: 'https://scrimba.com/c/cRbVZAB'
2018-09-30 23:01:58 +01:00
---
## Description
<section id='description'>
Another way to change the data in an array is with the <code>.pop()</code> function.
<code>.pop()</code> is used to "pop" a value off of the end of an array. We can store this "popped off" value by assigning it to a variable. In other words, <code>.pop()</code> removes the last element from an array and returns that element.
Any type of entry can be "popped" off of an array - numbers, strings, even nested arrays.
2019-05-17 06:20:30 -07:00
```js
var threeArr = [1, 4, 6];
var oneDown = threeArr.pop();
console.log(oneDown); // Returns 6
console.log(threeArr); // Returns [1, 4]
```
2018-09-30 23:01:58 +01:00
</section>
## Instructions
<section id='instructions'>
Use the <code>.pop()</code> function to remove the last item from <code>myArray</code>, assigning the "popped off" value to <code>removedFromMyArray</code>.
</section>
## Tests
<section id='tests'>
```yml
2018-10-04 14:37:37 +01:00
tests:
2018-10-20 21:02:47 +03:00
- text: <code>myArray</code> should only contain <code>[["John", 23]]</code>.
2019-07-13 00:07:53 -07:00
testString: assert((function(d){if(d[0][0] == 'John' && d[0][1] === 23 && d[1] == undefined){return true;}else{return false;}})(myArray));
2018-10-04 14:37:37 +01:00
- text: Use <code>pop()</code> on <code>myArray</code>
2019-07-13 00:07:53 -07:00
testString: assert(/removedFromMyArray\s*=\s*myArray\s*.\s*pop\s*(\s*)/.test(code));
2018-10-20 21:02:47 +03:00
- text: <code>removedFromMyArray</code> should only contain <code>["cat", 2]</code>.
2019-07-13 00:07:53 -07:00
testString: assert((function(d){if(d[0] == 'cat' && d[1] === 2 && d[2] == undefined){return true;}else{return false;}})(removedFromMyArray));
2018-09-30 23:01:58 +01:00
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
// Example
var ourArray = [1,2,3];
2018-10-08 01:01:53 +01:00
var removedFromOurArray = ourArray.pop();
2018-09-30 23:01:58 +01:00
// removedFromOurArray now equals 3, and ourArray now equals [1,2]
// Setup
var myArray = [["John", 23], ["cat", 2]];
// Only change code below this line.
var removedFromMyArray;
```
</div>
### After Test
<div id='js-teardown'>
```js
2018-10-20 21:02:47 +03:00
(function(y, z){return 'myArray = ' + JSON.stringify(y) + ' & removedFromMyArray = ' + JSON.stringify(z);})(myArray, removedFromMyArray);
2018-09-30 23:01:58 +01:00
```
</div>
</section>
## Solution
<section id='solution'>
```js
var myArray = [["John", 23], ["cat", 2]];
var removedFromMyArray = myArray.pop();
```
</section>