Skip to main content

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. 1

    Tick three times

    easy10 min

    Use setInterval to count to three, then stop and report the count.

  2. 2

    Count down

    easy10 min

    Count down from three and report liftoff at the end.

  3. 3

    Stop after one run

    easy10 min

    Clear the interval inside its own first run.

  4. 4

    Build a total

    easy10 min

    Add one each tick until the total reaches five, then report it.

  5. 5

    Repeat with setTimeout instead

    medium10 min

    Use a repeating setTimeout so each run schedules the next.

Find the bug(5)

  1. 1

    Fix the interval that never stops

    easy8 min

    Without clearInterval the count keeps rising past three.

  2. 2

    Fix the id not kept

    easy8 min

    clearInterval needs the id that setInterval returned.

  3. 3

    Fix setTimeout used where a repeat is needed

    easy8 min

    setTimeout runs once, so the count never reaches three.

  4. 4

    Fix the countdown ending early

    easy8 min

    The interval stops one step too soon.

  5. 5

    Fix the interval cleared before it runs

    easy8 min

    Clearing 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