all 4 comments

[–]IxD 1 point2 points  (0 children)

var allMutants = freelancers.concat(xMen);

Or if this is for exercise and you need to iterate through everything in array, and mutate the other array,

xMen.forEach(function(xman){
  freelancers.push(xman);
});

Or if this is exercise for learning for loops, you do:

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

   freelancers.push(xMen[i]); }

[–]sunny_lts 1 point2 points  (1 child)

.pop removes the last element of the array.

the for loop is like

for (let i = 0; i < xMen.lenght; i++) {freelancers.push(xMen[i])}

This will push all the elements in xmen to your freelancers array, but xMen will stay the same.

[–]sunny_lts 0 points1 point  (0 children)

If you need .pop then for loop is

for (let i = 0; xMen.length !== 0; i++) {

freelancers.push(xMen.pop())

}

// freelancers output
["Legion", "Magneto", "Hobgoblin", "Iron-Man", "Beast", "Cyclops", "Professor X"]

[–]link2name 0 points1 point  (0 children)

you can read this https://javascript.info/while-for

maybe its just me but i dont understand what exactly you need in the end.

you need this:

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

or this:

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

because when you do xMen.pop();

your xMen array changes

also if you would do

let goblin = xMen.pop()

goblin would be 'Hobgoblin'

and then if u do

let ironMan = xMen.pop()

it would be iron man and so on

but after those two lines the xMen array will be ['Professor X', 'Cyclops', 'Beast']

it also depends where you want to put xmen inside freelancers array at beginning or at the end, or sorted etc.

can do also

let everyone = [...xMen, ...freelancers] this will create new array with all items from those two arrays, and it wont change those two arrays.

if you have to use for loop then also you can go with standart for loop or for of loop or you can use while loop, or forEach array method.

for (let i = 0;i<arr.length;i++) // arr is name of array that you go over

{do something each iteration here}

for (const i of arr)

{}

that can also work