Created
August 1, 2015 16:08
-
-
Save chrisbodhi/f65aeee339eb30f2720b to your computer and use it in GitHub Desktop.
What will the value of `i` be in the alert box when `first()` and `tenth()` are called?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function makeGalaxy(size) { | |
| var stars = []; | |
| for (var i = 0; i < size; i++) { | |
| stars.push(function(){alert("This is star #" + i);}); | |
| } | |
| return stars; | |
| }; | |
| var galaxy = makeGalaxy(20); | |
| var first = galaxy[0]; | |
| first(); | |
| var tenth = galaxy[9]; | |
| tenth(); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Changes to
makeGalaxyto have it behave as expected: