Estou com uma duvida em for loop

3 respostas
javascript
Z

Using the methods you have learned, modify the xMen array already declared in the editor so that only members of the X-Men are present. os metodos que eu aprende sao: pop() and push().

Then add all of the mutants present in the xMen array to the freelancers array.
veja o que eu fiz

var xMen = ['Professor X', 'Cyclops', 'Beast', 'Iron-Man', 'Hobgoblin'];
    var freelancers = ['Legion', 'Magneto'];
    xMen.pop(3);
    xMen.pop(4);
    for (i=0;i<xfreelancers.push;i++) {
      console.log (freelancers[i]);
    }

olha o erro:

Code is incorrect

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

Legion
Magneto
undefined

3 Respostas

L

For an array with 5 elements, when you call pop twice on same array, and the second pop argument is 4, the error is because the array, at this moment of executing the second pop, doesn’t has 5 elements anymore.

Meu inglês é uma merda, tentei responder pq vc perguntou em inglês. Aliás, pq vc está perguntando em inglês?

Z

nao estou a conseguir uzar for loop para adicionar os elementos da array xMen para array de freelancers. sera que sabes fazer ?

D

Blz @Zelito.

tente assim.

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(xMen[i]);

console.log(freelancers);

}
Criado 15 de fevereiro de 2020
Ultima resposta 4 de mar. de 2020
Respostas 3
Participantes 3