en.javascript.info/1-js/06-advanced-functions/03-closure/10-make-army/task.md
Ilya Kantor c65a1e85b8 WIP
2020-09-10 19:02:34 +03:00

814 B

importance: 5


Army of functions

The following code creates an array of shooters.

Every function is meant to output its number. But something is wrong...

function makeArmy() {
  let shooters = [];

  let i = 0;
  while (i < 10) {
    let shooter = function() { // create a shooter function,
      alert( i ); // that should show its number
    };
    shooters.push(shooter); // and add it to the array
    i++;
  }

  // ...and return the array of shooters
  return shooters;
}

let army = makeArmy();

*!*
// all shooters show 10 instead of their numbers 0, 1, 2, 3...
army[0](); // 10 from the shooter number 0
army[1](); // 10 from the shooter number 1
army[2](); // 10 ...and so on.
*/!*

Why do all of the shooters show the same value?

Fix the code so that they work as intended.