Recall & Review
beginner
What is a nested loop in JavaScript?
A nested loop is a loop inside another loop. The inner loop runs completely every time the outer loop runs once.
Click to reveal answer
beginner
How many times will the inner loop run in this code?<br><pre>for(let i = 0; i < 3; i++) {<br> for(let j = 0; j < 2; j++) {<br> console.log(i, j);<br> }<br>}</pre>The inner loop runs 2 times for each of the 3 outer loop runs, so 3 × 2 = 6 times total.
Click to reveal answer
beginner
Why use nested loops? Give a real-life example.
Nested loops help when you need to check combinations, like rows and columns in a grid or seats in a theater. For example, checking every seat in every row.
Click to reveal answer
intermediate
What is the output of this nested loop?<br><pre>for(let i = 1; i <= 2; i++) {<br> for(let j = 1; j <= 3; j++) {<br> console.log(i * j);<br> }<br>}</pre>The output is:<br>1<br>2<br>3<br>2<br>4<br>6<br>Because for i=1, j runs 1 to 3, printing 1×1, 1×2, 1×3; then for i=2, j runs 1 to 3, printing 2×1, 2×2, 2×3.
Click to reveal answer
intermediate
How can nested loops affect performance?
Nested loops can slow down programs because the total steps multiply. For example, two loops each running 100 times means 10,000 steps. Use them carefully.
Click to reveal answer
What does a nested loop mean in JavaScript?
✗ Incorrect
A nested loop is when one loop is placed inside another loop, so the inner loop runs completely for each iteration of the outer loop.
How many times will the inner loop run in this code?<br>
for(let i=0; i<4; i++) {<br> for(let j=0; j<3; j++) {<br> // code<br> }<br>}✗ Incorrect
The inner loop runs 3 times for each of the 4 outer loop runs, so 4 × 3 = 12 times.
Which real-life example fits nested loops best?
✗ Incorrect
Nested loops are great for checking combinations like rows and seats, which is like checking every seat in every row.
What is the output of this code?<br>
for(let i=1; i<=2; i++) {<br> for(let j=1; j<=2; j++) {<br> console.log(i + j);<br> }<br>}✗ Incorrect
The sums are: i=1,j=1 → 2; i=1,j=2 → 3; i=2,j=1 → 3; i=2,j=2 → 4.
Why should you be careful with nested loops?
✗ Incorrect
Nested loops multiply the number of steps, which can slow down your program if loops run many times.
Explain what nested loops are and give a simple example in JavaScript.
Think about a loop inside another loop and how many times the inner loop runs.
You got /3 concepts.
Describe a real-life situation where nested loops would be useful and why.
Think about checking combinations or pairs, like rows and columns.
You got /3 concepts.