Last active
April 5, 2022 12:12
-
-
Save sqveeze/a0f5d2d2e812a36e41f9b26698f14d1d to your computer and use it in GitHub Desktop.
ES2K16
This file contains 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
// 1) What will be printed in the console? Answer is in line 100 | |
var arr = []; | |
for (var i = 1; i <= 5; i++) { | |
var y = i; | |
arr.push(function () { | |
console.log(y); | |
}) | |
} | |
arr.forEach((func) => { | |
func() | |
}); | |
// If you tought it will be: 1 2 3 4 5, then you are wrong. The answer is 5 5 5 5 5. | |
// 2) How to fix this code? Answer is in line 200 | |
// Change var y to let y | |
// 3) Why is this tiny change fix the code? Answer is in line 300 | |
// let is a block scoped variable while var can be accessed from outside the function. | |
// Thank you! Hope you enjoyed this little puzzle :) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment