JavaScript setInterval — exercises and bug fixes
5 hands-on exercises and 5 find-the-bug challenges for the JavaScript setInterval lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.
Exercises(5)
- 1
Tick three times
easy10 minUse setInterval to count to three, then stop and report the count.
- 2
Count down
easy10 minCount down from three and report liftoff at the end.
- 3
Stop after one run
easy10 minClear the interval inside its own first run.
- 4
Build a total
easy10 minAdd one each tick until the total reaches five, then report it.
- 5
Repeat with setTimeout instead
medium10 minUse a repeating setTimeout so each run schedules the next.
Find the bug(5)
- 1
Fix the interval that never stops
easy8 minWithout clearInterval the count keeps rising past three.
- 2
Fix the id not kept
easy8 minclearInterval needs the id that setInterval returned.
- 3
Fix setTimeout used where a repeat is needed
easy8 minsetTimeout runs once, so the count never reaches three.
- 4
Fix the countdown ending early
easy8 minThe interval stops one step too soon.
- 5
Fix the interval cleared before it runs
easy8 minClearing it straight away means the callback never fires at all.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspace