Use setInterval to count to three, then stop and report the count.
Acceptance criteria
- 1.#out should show 3.
JavaScript Tutorial · JavaScript Asynchronous
These 10 tasks go with the JavaScript setInterval chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “setInterval runs a function over and over until you stop it.” .
1.Tick three timesexercise · easy · 10 min
Use setInterval to count to three, then stop and report the count.
Passes when: #out should show 3.
2.Count downexercise · easy · 10 min · practice plan
Count down from three and report liftoff at the end.
Passes when: #out should show 3 2 1 liftoff.
3.Stop after one runexercise · easy · 10 min · practice plan
Clear the interval inside its own first run.
Passes when: #out should show ran once.
4.Build a totalexercise · easy · 10 min · practice plan
Add one each tick until the total reaches five, then report it.
Passes when: #out should show 5.
5.Repeat with setTimeout insteadexercise · medium · 10 min · practice plan
Use a repeating setTimeout so each run schedules the next.
Passes when: #out should show run 3.
6.Fix the interval that never stopsbug fix · easy · 8 min
Without clearInterval the count keeps rising past three.
Passes when: #out should show 3.
7.Fix the id not keptbug fix · easy · 8 min · practice plan
clearInterval needs the id that setInterval returned.
Passes when: #out should show stopped.
8.Fix setTimeout used where a repeat is neededbug fix · easy · 8 min · practice plan
setTimeout runs once, so the count never reaches three.
Passes when: #out should show 3.
9.Fix the countdown ending earlybug fix · easy · 8 min · practice plan
The interval stops one step too soon.
Passes when: #out should show 3 2 1 liftoff.
10.Fix the interval cleared before it runsbug fix · easy · 8 min · practice plan
Clearing it straight away means the callback never fires at all.
Passes when: #out should show ran once.
Between them these tasks cover setInterval, clearInterval and Repeating work.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.