728x90
// function makeArmy() {
// let shooters = [];
// let i = 0;
// while (i < 10) {
// const copyIndex = i;
// let shooter = function() { // create a shooter function,
// console.log( copyIndex ); // that should show its number
// };
// shooters.push(shooter); // and add it to the array
// i++;
// }
// // ...and return the array of shooters
// return shooters;
// }
function makeArmy() {
let shooters = [];
for(let i = 0; i < 10; i++) {
const shooter = function() {
console.log( i );
};
shooters.push(shooter);
}
return shooters;
}
let army = makeArmy();
army[0](); // 0
army[1](); // 1
army[2](); // 2
army[3](); // 3
army[4](); // 4
army[5](); // 5
army[6](); // 6
army[7](); // 7
army[8](); // 8
army[9](); // 9

+ Recent posts