Loops that you already know how to work with can be nested inside each other. For example, let's solve the following problem - display the string:
111222333444555666777888999
Single loop is not enough here - you need to run two nested loops: the first loop will iterate over the numbers, and the second one will repeat these numbers three times. Let's implement it:
for (let i = 1; i <= 9; i++) {
for (let j = 1; j <= 3; j++) {
document.write(i);
}
}
Please note: the first loop has a counter
i
, the second j
, and if
there is also the third loop, then the
variable k
will be its counter.
These are the standard common names
and should be used.
Use two nested loops to display the following string:
111222333444555666777888999
Use two nested loops to display the following string:
11 12 13 21 22 23 31 32 33