Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

221
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!