JavaScript traps · Chapter 5 of 16
Closures in loops
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Everyday traps
- 3 tasks
- JavaScript
- In the all labs plan
The first 2 chapters of JavaScript traps are free. This one comes with the all labs plan.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
repro.js: crash the rain reminders on purpose
Write reproduce() in repro.js, with no real waiting. Make two or more riders, each { name, phone }, a fake sms whose send(phone, text) keeps the phone in a list, and a fake schedule(fn, ms) that keeps fn in a list and does not run it. Call scheduleReminders(riders, sms, schedule) from reminders.js. Then run every kept callback yourself, inside a try: if one throws, resolve to the error's message; if none does, resolve to the list of phones messaged.
The check runs your reproduce() twice: beside the original reminders.js it must resolve to "Cannot read properties of undefined (reading 'phone')" (the crash from the ticket), and beside a fixed reminders.js it must resolve to the list of phones messaged, two or more, every one different. So it must really run the callbacks scheduleReminders handed your schedule. reproduce() must finish within 2 seconds.
- Task 2
reminders.js: every rider gets their own reminder
Fix scheduleReminders(riders, sms, schedule) in reminders.js. For the rider at index k it must schedule one callback k minutes from now (k * 60000 ms: the first at 0, the next at 60000, and so on). When a callback runs, it sends exactly one SMS to that rider's own phone with reminderText(that rider's name), and logs reminder sent to <name>. Nothing is sent before its callback runs. schedule defaults to setTimeout.
Keep reminderText, the gap and the log line as they are. The check passes its own schedule, keeps the callbacks and runs them later, last first, and also tries the default setTimeout with fake timers. A fix that skips the missing rider, or sends every rider the last rider's message, is as wrong as the crash.
- Task 3
reminders.test.js: a test that would have caught it
Write a vitest test file, reminders.test.js, that would have caught this bug. The check runs your file three times: beside the original reminders.js, where at least one test must fail; beside the fixed reminders.js, where every test must pass; and beside a wrong fix that sends every rider the last rider's message (no crash), where at least one test must fail.
So schedule reminders for two or more riders with a fake schedule that keeps the callbacks, run them after the call, as in your repro, and check that each rider's own phone got reminderText with their own name. Checking only that nothing throws, or that one SMS per rider went, misses the wrong fix. Each test must finish within 3 seconds.
What you will learn
- repro.js: crash the rain reminders on purpose
- reminders.js: every rider gets their own reminder
- reminders.test.js: a test that would have caught it
More in Everyday traps
Build "Closures in loops" yourself
Tapri runs on JavaScript, and Kabir, its senior developer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of JavaScript traps are free. This one comes with the all labs plan.