Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 2.5 — Loops: for, while and do...while
JavaScript · Example 4 of 4

Nested loops: a times table

The inner loop runs 5 times for each of the 5 rows: 5 × 5 = 25. Set size to 10 and it becomes 100: four times the work for twice the size.

Loops repeat a block of code: for when you know how to count the repeats, while when you repeat until a condition changes, and do...while when the body must run at least once. Most loop bugs are off-by-one errors and conditions that never become false.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Print the numbers from 10 down to 1, then Lift off!.

02

Using a while loop, find how many times you can halve 1000 (with / 2) before it is less than 1.

03

Print a staircase of # signs with 4 rows: #, ##, ###, ####.

Explain it without notes

01

Describe the three parts of a for loop and the order in which they run.

02

When would you choose while over for, and do...while over while?

03

What is an off-by-one error? Give an example and how to avoid it.

04

Why does an infinite loop freeze the whole page?

Nested loops: a times table

Preview

Press Run to see the page.

Console

Nothing logged yet.

Expected console output

cells: 25