closure
This commit is contained in:
parent
5b195795da
commit
964ed57c42
39 changed files with 365 additions and 461 deletions
35
1-js/06-advanced-functions/03-closure/10-make-army/task.md
Normal file
35
1-js/06-advanced-functions/03-closure/10-make-army/task.md
Normal file
|
@ -0,0 +1,35 @@
|
|||
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...
|
||||
|
||||
```js run
|
||||
function makeArmy() {
|
||||
let shooters = [];
|
||||
|
||||
let i = 0;
|
||||
while (i < 10) {
|
||||
let shooter = function() { // shooter function
|
||||
alert( i ); // should show its number
|
||||
};
|
||||
shooters.push(shooter);
|
||||
i++;
|
||||
}
|
||||
|
||||
return shooters;
|
||||
}
|
||||
|
||||
let army = makeArmy();
|
||||
|
||||
army[0](); // the shooter number 0 shows 10
|
||||
army[5](); // and number 5 also outputs 10...
|
||||
// ... all shooters show 10 instead of their 0, 1, 2, 3...
|
||||
```
|
||||
|
||||
Why do all of the shooters show the same value? Fix the code so that they work as intended.
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue