Q
Q
Qurel2020-05-01 09:55:53
JavaScript
Qurel, 2020-05-01 09:55:53

How to output such a sequence using nested loops for any number of iterations of the outer loop?

With the help of nested loops, draw a string:
10x01x
10x01x
10x01x

For example, the symbol "x" - is displayed every third iteration of the loop, how to write it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sokolov, 2020-05-01
@Qurel

output every third iteration of the loop, how to write this?

You can check the remainder after dividing the counter by 3, the %
Remainder operator will give 0 every third iteration:
for (let i = 0; i < 5; i++) {
  for (let j = 0; j < 10; j++) {
    if (j % 3 === 0) {
      console.log("Это 0, 3, 6, 9, ... итерация внутреннего цикла");
    }

    if (j % 3 === 2) {
      console.log("Это 2, 5, 8, ... итерация внутреннего цикла");
    }
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question