Pop and Push in a loop

Using the methods you have learned (arrays pop and push), modify the xMen array already declared in the editor so that only members of the X-Men are present.

Then add all of the mutants present in the xMen array to the freelancers array.

I think this last part is what I am missing.


ok, scrap everything I said above

you change the xMen array
then the xMen array now has 3 elements

you add those elements to freelancers array

Getting closer I think:

var xMen = ['Professor X', 'Cyclops', 'Beast', 'Iron-Man', 'Hobgoblin'];
var freelancers = ['Legion', 'Magneto'];


for (var i = 0; i < xMen.length; i++)

The error with result bellow:

>>>>Code is incorrect
You should add the remaining mutants in the xMen array to the freelancers array, and you should access those mutants through the i index
"Professor X",
"Professor X",
"Professor X"

I understand that the loop is going over for three times always adding same index 0 and that why same name appears three times.
To iterate through the index is next step right?

what would be the next step?

1 Like

To change the 0 for i!!! :sweat_smile:

Thank you ieahlenn

From what you said that the exercise is…
You have xmen array. They are saying that only 1st 3 items are xmen. So from where you will start to loop through the array?
What a pop() function is doing? Is returning something?
What would be the length of xmen array when you pop out each time? What you need to do in this case?
You need to think what is happening there otherwise you can’t only memorize. console.log is your friend to test things