Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Challenge: Stop and Skip in Loops | Looping Through Data in JavaScript
Introduction to JavaScript

book
Challenge: Stop and Skip in Loops

Task

Implement a loop that skips even iterations and stops on the 5th iteration. Here are the instructions:

  1. The loop should stop at the 5th iteration.

  2. For each iteration, output the iteration number to the console.

  3. If the loop skips an iteration, output the word "Skip" to the console.

  4. If the loop stops, output the word "Stop" to the console.

for (let i = 1; i <= 10; i++) {
console.log("Iteration", ___);
if (i >= 5) {
console.log("___");
___;
};

if (i % 2 == 0) {
console.log("___");
___;
} else {
console.log("Successful");
};
};
123456789101112131415
for (let i = 1; i <= 10; i++) { console.log("Iteration", ___); if (i >= 5) { console.log("___"); ___; }; if (i % 2 == 0) { console.log("___"); ___; } else { console.log("Successful"); }; };
copy

The output should be:

python
Iteration 1
Successful
Iteration 2
Skip
Iteration 3
Successful
Iteration 4
Skip
Iteration 5
Stop
  1. Include the counter variable in the first console.log() statement.

  2. Use the break statement inside the if (i >= 5) block to stop the loop.

  3. Output the string "Stop" to the console before the break statement.

  4. Use the continue statement inside the if (i % 2 == 0) block to skip even iterations.

  5. Output the string "Skip" to the console before the continue statement.

  6. For other cases, output "Successful" to the console.

for (let i = 1; i <= 10; i++) {
console.log("Iteration", i);

if (i >= 5) {
console.log("Stop");
break;
}

if (i % 2 == 0) {
console.log("Skip");
continue;
} else {
console.log("Successful");
}
}
123456789101112131415
for (let i = 1; i <= 10; i++) { console.log("Iteration", i); if (i >= 5) { console.log("Stop"); break; } if (i % 2 == 0) { console.log("Skip"); continue; } else { console.log("Successful"); } }
copy

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 5. Capítulo 8

Pergunte à IA

expand
ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

some-alt