All 35 Bugs /
0/35 Fixed ✓
JavaScriptEasy~8 min

Stale Loop Closure in setTimeout

What's Broken (Symptom)

Calling `scheduleLogs()` resolves with `[3, 3, 3]` instead of `[0, 1, 2]` because every callback shares the same function-scoped variable.

Expected Behavior

  • `scheduleLogs(3)` resolves asynchronously to `[0, 1, 2]`.
  • `scheduleLogs(5)` resolves cleanly to `[0, 1, 2, 3, 4]`.
  • Uses proper per-iteration block scoping (`let`) or closure capture.

Debugging Hints (0/2 used)

Try spotting the bug in the editor first. Need a nudge? Reveal Hint 1 anytime.

Root Cause Breakdown

Pass all 3 automated verification checks on the right to unlock the full Root Cause explanation.

Automated Bug Verification

3 Checks Ready
  • scheduleLogs(3) outputs [0, 1, 2]Verify each timer captures 0, 1, 2 in sequence.
  • scheduleLogs(5) outputs [0, 1, 2, 3, 4]Works dynamically for any loop count.
  • Avoids shared var loop bindingLoop should use block scoping.
bugfix.js
Live Sandboxed PreviewCtrl + Enter to verify
Console Output0 log(s)
No console output yet. Run Preview or Verify Bug Fix.