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
Count to four
easy10 minUse a while loop to count 1,2,3,4 and join them with commas.
- 2
Halve until small
easy10 minStarting at 40, halve repeatedly while above 5, then show the value.
- 3
Sum while under a limit
easy10 minAdd 1,2,3... while the running total stays below 10, then show the total.
- 4
Drain a list
easy10 minRemove items from [1,2,3] until it is empty, then show the count removed.
- 5
Find the first multiple
medium10 minFind the first number above 10 divisible by 7.
Find the bug(5)
- 1
Fix the condition that never runs
easy8 minThe comparison points the wrong way, so the loop body is skipped.
- 2
Fix the wrong start value
easy8 minThe counter starts too high, so the first numbers are missing.
- 3
Fix the counter that moves the wrong way
easy8 minDecrementing means the loop exits immediately.
- 4
Fix the wrong divisor test
easy8 minThis searches for a multiple of the wrong number.
- 5
Fix the emptied list check
easy8 minThe 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