JavaScript for Loop — exercises and bug fixes
10 hands-on exercises and 10 find-the-bug challenges for the JavaScript for Loop lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.
Exercises(10)
- 1
Render a numbered list with a loop
easy15 minUse a for loop to append 5 <li> elements to #list, with text "Item 1" through "Item 5".
- Add 5 list items.
- The first item should read "Item 1".
- The fifth item should read "Item 5".
- 2
Sum numbers with a loop
easy15 minUse a for loop to add up every whole number from 1 to 10, then display the total inside #total as "Total: 55".
- 3
Build a multiplication table
easy15 minUse a for loop to add 5 rows to #table-body, each with two cells: the multiplier (1-5) and its product with 3 (3, 6, 9, 12, 15).
- Add 5 rows to the table body.
- The first row's product should be 3.
- The fifth row's product should be 15.
- 4
Count down with a loop
easy15 minUse a for loop to add 5 <li> elements to #countdown counting down from 5 to 1 (in that order).
- Add 5 items to the countdown.
- The first item should be 5.
- The last item should be 1.
- 5
List only even numbers
easy15 minUse a for loop that steps by 2 to add the even numbers 2, 4, 6, 8, 10 as <li> elements to #evens.
- Add 5 even numbers.
- The first even number should be 2.
- The last even number should be 10.
- 6
Sum one to five
easy10 minAdd the numbers 1 to 5 and show the total.
- 7
Count the items
easy10 minCount how many names are in the list and show the number.
- 8
Build a countdown
easy10 minProduce "3,2,1" by counting down from three.
- 9
Multiply through a list
easy10 minMultiply 2, 3 and 4 together and show the product.
- 10
Sum only the even numbers
medium10 minFrom 1 to 6, add just the even values.
Find the bug(10)
- 1
Fix the loop that stops one item early
easy15 minThis loop is supposed to add 5 list items, but its condition stops it one item short. Fix the loop condition.
- The list should have 5 items.
- The fifth item should read "Item 5".
- 2
Fix the sum that's missing its last number
easy15 minThis loop should sum 1 through 10 (total 55), but it stops before adding 10. Fix the loop condition so the total is correct.
- 3
Fix the multiplication table's missing first row
easy15 minThis multiplication-by-4 table should start at 1, but the loop starts at 2, so the first row is missing. Fix the starting value.
- The table should have 5 rows.
- The first row's product should be 4 (1 × 4).
- 4
Fix the countdown that stops too soon
easy15 minThis countdown should reach 1, but the loop condition stops it at 2. Fix the condition so it counts all the way down to 1.
- The countdown should have 5 items.
- The last item should be 1.
- 5
Fix the even-numbers list that shows odd numbers
easy15 minThis list should show only even numbers (2, 4, 6, 8, 10), but the loop increments by 1 instead of 2, so it lists 1-5 instead. Fix the step.
- The first item should be 2.
- The second item should be 4, not 3.
- 6
Fix the off-by-one bound
easy8 minThe loop stops one short, so the total is wrong.
- 7
Fix the start index
easy8 minStarting at 1 skips the first item of the array.
- 8
Fix the out-of-range bound
easy8 minUsing <= reads one past the end and produces NaN.
- 9
Fix the countdown direction
easy8 minThe loop counts the wrong way and never runs.
- 10
Fix the accumulator reset
easy8 minThe total is reset inside the loop, so only the last value survives.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspace