Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

223
Vistas
in JS I Don't Know this Expression
function makeArmy() {

  let shooters = [];

  for (let i = 0; i < 10; i++) {
    
    let shooter = function() {
      alert(i);
    };

    shooters.push(shooter);
  
  }
  
  return shooters;

}

let army = makeArmy();

army[0](); < -- - what is this ?

army[5](); < -- - what is this ?

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Let's do this back-to-front:

let army = makeArmy();

That calls a function...

// ...and the function
function makeArmy() {

  // Creates an array
  let shooters = [];

  // It loops from 0 to 9 pushing a new function
  // into the array on each iteration
  for (let i = 0; i < 10; i++) {

    // Create the new function
    let shooter = function() {

      // I used console.log because it's much less hassle :)
      console.log(i);

    };

    // Add the function to the array
    shooters.push(shooter);
  
  }
  
  // And then you return the array of functions
  return shooters;

}

// An array of functions!
let army = makeArmy();

// Call the first function in the array
army[0]();

// Call the fourth function in the array
army[3]();

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda