Skip to main content

JavaScript do while Loop — exercises and bug fixes

5 hands-on exercises and 5 find-the-bug challenges for the JavaScript do while Loop 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

    Run at least once

    easy10 min

    Use do...while with a false condition and show how many times the body ran.

  2. 2

    Count to three

    easy10 min

    Use do...while to collect 1,2,3.

  3. 3

    Add until over ten

    easy10 min

    Keep adding 4 until the total passes 10, then show the total.

  4. 4

    Double until large

    easy10 min

    Start at 3 and double until above 20.

  5. 5

    Compare with while

    medium10 min

    A while loop with a false condition runs zero times — show that count.

Find the bug(5)

  1. 1

    Fix the loop that skips the first pass

    easy8 min

    A plain while never runs here; do...while should run once.

  2. 2

    Fix the missing increment guard

    easy8 min

    The condition uses the wrong bound and stops too early.

  3. 3

    Fix the inverted exit test

    easy8 min

    The condition is reversed, so the loop stops after one pass.

  4. 4

    Fix the wrong operator

    easy8 min

    Adding instead of doubling gives the wrong result.

  5. 5

    Fix the counter placed after the check

    easy8 min

    The count is read before the loop updates it.

Ready to write the code?

The editor, the automatic checks and the worked solutions open up with an account.

Open the workspace