Count how many times the inner code runs for a 3 by 4 pair of loops.
Acceptance criteria
- 1.#out should show 12.
JavaScript Tutorial · JavaScript Loops
These 10 tasks go with the JavaScript Nested Loops chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “A JavaScript nested loop is a loop placed inside another loop.” .
1.Count the turnsexercise · easy · 10 min
Count how many times the inner code runs for a 3 by 4 pair of loops.
Passes when: #out should show 12.
2.Add up a gridexercise · easy · 10 min · practice plan
Add every number in the grid together and show the total.
Passes when: #out should show 21.
3.Build a star patternexercise · easy · 10 min · practice plan
Build 3 rows of stars, where row 1 has one star. Join the rows with a dash.
Passes when: #out should show *-**-***.
4.Pair two listsexercise · easy · 10 min · practice plan
Pair every name with every city and count how many pairs there are.
Passes when: #out should show 6.
5.Avoid repeating pairsexercise · medium · 10 min · practice plan
Count each pair of numbers only once by starting the inner loop after the outer one.
Passes when: #out should show 10.
6.Fix the shared loop variablebug fix · easy · 8 min
Both loops use i, so the inner loop overwrites the outer one.
Passes when: #out should show 12.
7.Fix the inner loop that never runsbug fix · easy · 8 min · practice plan
The inner loop starts above its limit, so nothing is added.
Passes when: #out should show 21.
8.Fix the row that is never resetbug fix · easy · 8 min · practice plan
The row string carries over, so every row keeps the stars before it.
Passes when: #out should show *-**-***.
9.Fix the break that stops too muchbug fix · easy · 8 min · practice plan
A break in the inner loop was meant to skip, not to end the pairing early.
Passes when: #out should show 6.
10.Fix the repeated pairsbug fix · easy · 8 min · practice plan
The inner loop starts at zero, so every pair is counted twice.
Passes when: #out should show 10, not 20.
Between them these tasks cover Nested loops, Rows and columns and Two dimensional data.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.