In this video, you packed a while loop into a for loop and followed it one pass at a time. The key points:
- A
forloop holds the same three parts as awhileloop, the start, the check and the update, in one header line, separated by semicolons. - The start runs once, before anything else. Then the loop repeats check, body, update, until the check is
false. - The update runs after the body and before the next check.
- The update decides which way the counter moves and how far:
round++counts up by one (short forround = round + 1),count--counts down by one (short forcount = count - 1), andscore += 10takes steps of 10 (short forscore = score + 10). - The check comes before every pass, the first one included, so a
forloop can run zero times.
The rounds loop from the video:
for (let round = 1; round <= 3; round++) {
console.log("Round", round);
}
console.log("Game over");
Round 1
Round 2
Round 3
Game over