Pop and Push in a loop

Hi,
having some troubles solving this exercise:

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

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

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

xMen.pop();
xMen.pop();

for (var i = 0; i < freelancers.length; i++);
{
    freelancers.push('Iron-Man', 'Hobgoblin');
}

console.log(freelancers);

Writting that code I Receive the following error:

Code is incorrect
There should be a for loop to add the elements to the freelancers array

Thanks for help!

you ahve an extra ; here so the loop is not executing, or better, nothign happens inside the loop

Yes you were right, removed it and now I get this output:

Output
>>>>Code is incorrect
The for loop should iterate until the value stored in i reaches the length of the array
Script execution timed out after 1000ms

Take a moment to think about what your code does:

First loop: i = 0 and freelancers.length = 2
–> push two items into the freelancers array

Second loop: i = 1 and freelancers.length = 4
–> push two items into the freelancers array

Do you see where this is going?

1 Like

The i will never reach the value stored in freelancers.lenght?

Trying to figure out out i will reach the lenght :confused:

instead of hardcoding the two strings to push, could you use the returned value from pop?

Answering to your question I believe its not what its supposed too, not sure.

I have now tried to use a break, to get out of the loop but gives the error bellow.

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

xMen.pop();
xMen.pop();

for (var i = 0; i < freelancers.length; i++)
{
    freelancers.push('Iron-Man','Hobgoblin');
    break;
}

console.log(freelancers);

Error:

Output
>>>>Code is incorrect
The for loop should iterate until the value stored in i reaches 
the length of the array

delete the loop, leave just the line with the push method, and you get the same result - this is not the way you are asked to do this then

you need to use the loop in a different way.

what does the pop method returns? can you use it?

The result I managed to get with two different ways.

What I cannot do is to stop the loop itself , cause on each iteration I keep adding two more itens. So the length will increase to and never stop.

If I try to say what you said they say I need to use a loop… :confused:

what does the pop method returns? how can you use that instead of hardcoding the strings?

How can I use the loop you mean?
I added the pop.

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



xMen.pop();
xMen.pop();

console.log(xMen);

for (var i = 0; i < freelancers.length; i++)
{
    freelancers.push('Iron-Man','Hobgoblin');

}


console.log(freelancers);

Bellow the error message with result:

Output
>>>>Code is incorrect
The for loop should iterate until the value stored in i reaches the length of the array
[
"Professor X",
"Cyclops",
"Beast"
]
Script execution timed out after 1000ms

where? nothing has changed

let popped = xMen.pop()
console.log(popped) // how can you use this?

I could use like this?

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

let popped = xMen.pop();
console.log(popped);

for (var i = 0; i < freelancers.length; i++)
{
    freelancers.push(popped);
}

console.log(freelancers);

no, you can’t, there is the infinite loop issue again there, also you are adding only the last element of xMen to freelancers

While we are primarily here to help people with their Free Code Camp progress, we are open to people on other paths, too. Some of what you are asking is pretty trivial in the Free Code Camp context, so you might find that if you’re not getting the instruction and material you need in your current studies, the FCC curriculum will really help you get started. At a modest guess I’d say investing a 4-5 hours working through the curriculum here will really pay off. You can find the curriculum at https://www.freecodecamp.org/learn.

Thank you for your suggestion about the curriculum. I will defintely use it. Meanwhile I am aiming to finish this exercises so I can be eligible to apply for a different kind of course.

You have another tip to make the freelancers not increase each iteration? Or not posible?

freelancers length will increasing during your code execution as you are adding two more elements to it

it’s a weird challenge, I would not iterate on an array that have to change length

there is written on which array you must iterate?

It doesnt say which array I have to iterate.

then maybe you can make your own array that doesn’t change length

Its weird this, now with this code I got following error:

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

xMen.pop();
xMen.pop();

for (var i = 0; i < xMen.length; i++)
{
    freelancers.push('Iron-Man', 'Hobgoblin');
    break;
    
}  
   console.log(freelancers);

This is the error with result bellow:

Output
>>>>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
[
"Legion",
"Magneto",
"Iron-Man",
"Hobgoblin"
]```

well, I don’t understand what this challenge wants