Javascript array pop() function

In the following example i have used the following code but its not working.
const myArray =[[“John”, 23], [“cat”, 2]];

i was ask to make myArray equals [[“John”, 23]];
after using pop() function on myArray .

then make const removedFromArray equals [[“cat”, 2]];

Your code so far
can’t solve this i need help

my code so far:

const myArray = [[“John”, 23], [“cat”, 2]];

var ab = myArray.pop();
var removedFromMyArray = myArray.pop();

Can you post a link to the lesson?

But before that

I think

var removedFromMyArray = myArray.pop();

would just return [“John”, 23] again. Because “pop” just removes the last element from the array. It doesn’t return the element that was removed.

Are you allowed to try

var removedFromMyArray = myArray.shift() ?

array.shift() removes the first element. That should give you [“cat”, 2]

Pop() indeed remove the last element and return the removed element.

myArray will be modified to contain the remaining elements.

Ah. My mistake, then.

After one pop(), myArray alread should contain the expected value and the result is the value that you need to pass to removedFromMyArray.

Don’t forget about declaring the variables as constants!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.