AniUI Academy
easy+150 XPPractice

Fix the classic loop closure bug

This is the bug that made let worth shipping. A loop builds one callback per item; because var i is function-scoped, all the callbacks close over the *same* i, and by the time any of them runs the loop has finished and i sits one past the end. Every handler reports the last index — or, as here, undefined.

The starter code contains exactly that bug. Fix makeLoggers(items) so it returns an array with one function per item, in order, where the function at index i returns the string i + ":" + items[i] — for example "0:a". The functions must be correct however many times and in whatever order they are called, and an empty input array must produce an empty array.

What it has to do

  • Return one function per item, in the same order as the input.
  • The function at index i returns i + ":" + items[i], so each closure captures its own index.
  • Calling the functions repeatedly or out of order returns the same results.
  • An empty input array returns an empty array.

Your workspace

Try it yourself
Loading playground...

Ready to check it?

4 tests run against your code, right here in your browser. Sign in to claim the XP when you pass.

AI Crack & Solution Assist

Stuck? Get instant AI hints or break down the optimal solution.

Stuck? The javascript course covers everything this challenge needs.