Skip to main content

JavaScript Nested Loops — exercises and bug fixes

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

    easy10 min

    Count how many times the inner code runs for a 3 by 4 pair of loops.

  2. 2

    Add up a grid

    easy10 min

    Add every number in the grid together and show the total.

  3. 3

    Build a star pattern

    easy10 min

    Build 3 rows of stars, where row 1 has one star. Join the rows with a dash.

  4. 4

    Pair two lists

    easy10 min

    Pair every name with every city and count how many pairs there are.

  5. 5

    Avoid repeating pairs

    medium10 min

    Count each pair of numbers only once by starting the inner loop after the outer one.

Find the bug(5)

  1. 1

    Fix the shared loop variable

    easy8 min

    Both loops use i, so the inner loop overwrites the outer one.

  2. 2

    Fix the inner loop that never runs

    easy8 min

    The inner loop starts above its limit, so nothing is added.

  3. 3

    Fix the row that is never reset

    easy8 min

    The row string carries over, so every row keeps the stars before it.

  4. 4

    Fix the break that stops too much

    easy8 min

    A break in the inner loop was meant to skip, not to end the pairing early.

  5. 5

    Fix the repeated pairs

    easy8 min

    The inner loop starts at zero, so every pair is counted twice.

Ready to write the code?

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

Open the workspace