$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · medium · #30

What is the correct fix?

var fns = [];
for (var i = 0; i < 3; i++) {
  fns.push(() => i);
}
console.log(fns.map(f => f()));
  1. Change () => i to () => i++
  2. Use fns[i] = i
  3. Move the push outside the loop
  4. Change var i to let i
let gives each iteration its own i; var shares one.
Play Problem Solving ▸