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
Count the turns
easy10 minCount how many times the inner code runs for a 3 by 4 pair of loops.
- 2
Add up a grid
easy10 minAdd every number in the grid together and show the total.
- 3
Build a star pattern
easy10 minBuild 3 rows of stars, where row 1 has one star. Join the rows with a dash.
- 4
Pair two lists
easy10 minPair every name with every city and count how many pairs there are.
- 5
Avoid repeating pairs
medium10 minCount each pair of numbers only once by starting the inner loop after the outer one.
Find the bug(5)
- 1
Fix the shared loop variable
easy8 minBoth loops use i, so the inner loop overwrites the outer one.
- 2
Fix the inner loop that never runs
easy8 minThe inner loop starts above its limit, so nothing is added.
- 3
Fix the row that is never reset
easy8 minThe row string carries over, so every row keeps the stars before it.
- 4
Fix the break that stops too much
easy8 minA break in the inner loop was meant to skip, not to end the pairing early.
- 5
Fix the repeated pairs
easy8 minThe 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