Skip to main content

JavaScript while Loop — exercises and bug fixes

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

    Count to four

    easy10 min

    Use a while loop to count 1,2,3,4 and join them with commas.

  2. 2

    Halve until small

    easy10 min

    Starting at 40, halve repeatedly while above 5, then show the value.

  3. 3

    Sum while under a limit

    easy10 min

    Add 1,2,3... while the running total stays below 10, then show the total.

  4. 4

    Drain a list

    easy10 min

    Remove items from [1,2,3] until it is empty, then show the count removed.

  5. 5

    Find the first multiple

    medium10 min

    Find the first number above 10 divisible by 7.

Find the bug(5)

  1. 1

    Fix the condition that never runs

    easy8 min

    The comparison points the wrong way, so the loop body is skipped.

  2. 2

    Fix the wrong start value

    easy8 min

    The counter starts too high, so the first numbers are missing.

  3. 3

    Fix the counter that moves the wrong way

    easy8 min

    Decrementing means the loop exits immediately.

  4. 4

    Fix the wrong divisor test

    easy8 min

    This searches for a multiple of the wrong number.

  5. 5

    Fix the emptied list check

    easy8 min

    The loop checks the wrong end condition and removes nothing.

Ready to write the code?

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

Open the workspace