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:
let num = 1;
for (let i = 0; i < 5; i++) {
num = num * 2;
console.log(num);
}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:
let num = 1;
for (let i = 1; i <= 5; i++) {
num = num * i;
}
console.log(num);120
For adding, start at 0. The shortcut total += i means total = total + i:
let total = 0;
for (let i = 1; i <= 10; i++) {
total += i;
}
console.log(total);55
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:
for (let i = 0; i < 5; i++) {
console.log("sit-up");
if (i == 2) {
console.log("running");
}
}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:
for (let i = 1; i <= 3; i++) {
for (let j = 1; j <= 3; j++) {
console.log(i + " x " + j + " = " + i * j);
}
}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:
for (let row = 1; row <= 3; row++) {
let line = "";
for (let col = 1; col <= 5; col++) {
line = line + "*";
}
console.log(line);
}***** ***** *****
i and j). The inner counter starts again on every outer round, so the inner part runs 3 × 3 = 9 times.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.
Press Run to see the result. Work out 1×2×3×…×10 using the counter i (from 1 to 10), then print the result.
Press Run to see the result. 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.
Press Run to see the result. 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.
Press Run to see the result. The program prints lunch box five times. Add code that prints fruit right after the fourth time.
for (let i = 0; i < 5; i++) {
console.log("lunch box");
}Press Run to see the result. Add up only the even numbers from 1 to 20 and print the total once at the end.
Hint
A number is even wheni % 2 == 0.Press Run to see the result. Print this staircase with two nested loops (line number r has r stars):**********
Hint
Make the inner conditioncol <= row.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 * iortotal += i. ifinside 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.