Manipulate Arrays With pop() how to use the popped info

Tell us what’s happening:
the quest is advising us to pop the myArray and make the popped value = to the removed variable im just stuck on what to do next. i dont understand with the info that was given how to successfully complete this challenge. any helpers?

Your code so far


// Example
var ourArray = [1,2,3];
var removedFromOurArray = ourArray.pop(); 
// 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 = myArray;
myArray.pop();


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/manipulate-arrays-with-pop

Everything you need to know is right here:

.pop() 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

var threeArr = [1, 4, 6];
---var oneDown = threeArr.pop();---
console.log(oneDown); // Returns 6

In the above example (from the actual exercise), what’s happening?

  1. An array threeArr is defined,
  2. The pop() function is applied to ‘pop off’ (break off, remove, if you don’t like ‘pop off’) the last element from the array threeArr,
  3. The value of that ‘popped off’ element is stored to the variable oneDown.
  4. When oneDown is logged to the console to be printed, we see that it has the value of the last element of the array threeArr.
1 Like

Is my,array a 2 dimensional array? Why myarray:myarray.pop(). Is it piping both elements from the array? Index 1 and index [1,1] and indext[1,2]?

I haven’t started these lessons.

Please, could you clarify what the last element means? Does this mean the last item in a multi - dimensional array or the last array in a multi-dimensional array?

In one exercise, it popped off the last item whilst in another it removed an entire array in a multi-dimensional array?
Some clarification would be appreciated.
Many thanks

Elements in an array can be any data type, so if the last element is an array that is what is popped out, if it is a number that is what is popped out, etc

1 Like

Please, could you clarify what the last element means? Does this mean the last item in a multi - dimensional array or the last array in a multi-dimensional array?

From the Wikipedia page for “Array data structure”:

In computer science, an array data structure , or simply an array , is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key .

Consider the following code block:

let arr = [["a", "b"], ["c", "d"], ["e", "f"]]
console.log(arr[2])

According to the definition above, if the last subarray ["e", "f"] is the “last element” in the array arr, then console.log(arr[2]) should give us ["e", "f"]

Run the code, and the console shows:

[Running] node 
[ 'e', 'f' ]

[Done] exited with code=0 in 0.138 seconds

So, what we’ve called the “last element” in the array arr according to the Wikipedia definition, is an array itself.
If we extend our code block:

let arr = [["a", "b"], ["c", "d"], ["e", "f"]]
let poppedElement = arr.pop()
console.log(poppedElement)

and run it, we see in the console:

[Running] node 
[ 'e', 'f' ]

[Done] exited with code=0 in 0.142 seconds

In other words, the element popped off the end of the array arr is an array.
arr is a multi-dimensional array and the “last element” in that multi-dimensional array is an array. But, this is so due to the way we defined our array arr.

If we define our array a different way:

let arr = [["a", "b"], ["c", "d"], ["e", "f"], "z"]
let poppedElement = arr.pop()
console.log(poppedElement)

and run the code, we get logged to the console:

[Running] node 
z

[Done] exited with code=0 in 0.149 seconds

Hope this helps.

Sources:

1 Like

Thanks very much for explaining this. I’ve got it!

Thanks for your help! It is now clear.