Frontend101 logoFrontend101
← Problems
#18EasyQuiz

18. Closures & Loop Scoping (var vs let)

JavaScriptClosures & Scope

Fix a classic loop closure bug so each callback captures its own iteration index instead of sharing the final loop value.

Example 1:
const fns = createIndexCallbacks(3);
fns.map((fn) => fn()); // [0, 1, 2]
solution.js
Each callback returns its own iteration index
expect(createIndexCallbacks(3).map((fn) => fn())).toEqual([0, 1, 2])