Skip to content

Instantly share code, notes, and snippets.

@marklin-latte
Last active September 18, 2020 13:07
Show Gist options
  • Save marklin-latte/86940c19dee18a924e654264004fe89b to your computer and use it in GitHub Desktop.
Save marklin-latte/86940c19dee18a924e654264004fe89b to your computer and use it in GitHub Desktop.
// Bug Version
function createArrayOfFunctions(y) {
var arr = [];
for(var i = 0; i<y; i++) {
arr[i] = function(x) { return x + i; } // <=== the i is bug ! the i will always be y+1;
}
return arr;
}
// Current Version
function createArrayOfFunctions(y) {
var arr = [];
for(var i = 0; i<y; i++) {
arr[i] = ((i) => {
return function(x){return x + i;}
})(i);
}
return arr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment