JavaScript / Loops

Repeated calculations and nested loops

Add and multiply with a for loop, run a command on a chosen round with if, and put a loop inside a loop.

A loop can repeat any operation, not only printing. To work out 2×2×2×2×2, start with num = 1 and multiply it by 2 five times; the variable carries the result from one round to the next:

JavaScript
let num = 1;
for (let i = 0; i < 5; i++) {
  num = num * 2;
  console.log(num);
}
Console
2
4
8
16
32

You can use the counter itself in the calculation. Here i takes the values 1 to 5, so the program works out 1×2×3×4×5 and prints only the final result, because the print is after the loop:

JavaScript
let num = 1;
for (let i = 1; i <= 5; i++) {
  num = num * i;
}
console.log(num);
Console
120

For adding, start at 0. The shortcut total += i means total = total + i:

JavaScript
let total = 0;
for (let i = 1; i <= 10; i++) {
  total += i;
}
console.log(total);
Console
55
For multiplying, start the variable at 1 (multiplying by 0 always gives 0); for adding, start at 0. Declare it before the loop, or it starts over every round.

if inside a loop

Putting if inside a loop runs a command on chosen rounds only. Here there are five sit-ups, with a run after the third one. The counter starts at 0, so the third time is i == 2:

JavaScript
for (let i = 0; i < 5; i++) {
  console.log("sit-up");
  if (i == 2) {
    console.log("running");
  }
}
Console
sit-up
sit-up
sit-up
running
sit-up
sit-up

A loop inside a loop

You can put a whole loop inside another one. On every round of the outer loop, the inner loop runs from start to finish. This prints a small times table:

JavaScript
for (let i = 1; i <= 3; i++) {
  for (let j = 1; j <= 3; j++) {
    console.log(i + " x " + j + " = " + i * j);
  }
}
Console
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9

To draw a shape, the inner loop builds each line by adding "*" to an empty text, and the outer loop prints it:

JavaScript
for (let row = 1; row <= 3; row++) {
  let line = "";
  for (let col = 1; col <= 5; col++) {
    line = line + "*";
  }
  console.log(line);
}
Console
*****
*****
*****
Give each loop a counter with a different name (i and j). The inner counter starts again on every outer round, so the inner part runs 3 × 3 = 9 times.

Practice

1Write the programCurriculum practice

Work out 7×7×7×7×7: declare num = 1, repeat num = num * 7 five times (i from 0 while less than 5), then print num once at the end.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
2Write the programCurriculum practice

Work out 1×2×3×…×10 using the counter i (from 1 to 10), then print the result.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
3Write the programCurriculum practice

Work out 0+1+2+3+4+5: declare num = 0, repeat num = num + i six times (i from 0 while less than 6), then print num.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
4Write the programCurriculum practice

Work out 5040÷1÷2÷3÷4÷5: start with num = 5040, repeat num = num / i with i from 1 to 5, then print num.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
5Edit the programCurriculum practice

The program prints lunch box five times. Add code that prints fruit right after the fourth time.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
for (let i = 0; i < 5; i++) {
  console.log("lunch box");
}
Console
Press Run to see the result.
6Write the programExtra practice

Add up only the even numbers from 1 to 20 and print the total once at the end.

HintA number is even when i % 2 == 0.
JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
7Write the programExtra practice

Print this staircase with two nested loops (line number r has r stars):
*
**
***
****

HintMake the inner condition col <= row.
JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.

Key points

  • To repeat a calculation, declare the variable before the loop (1 for multiplying, 0 for adding) and update it inside, e.g. num = num * i or total += i.
  • if inside a loop runs a command on chosen rounds, e.g. if (i == 2).
  • In nested loops the inner loop runs completely on every outer round, and each has its own counter.