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

bookChallenge: 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.
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:

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.
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

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 8

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain why the loop stops at the 5th iteration?

What would happen if we changed the stopping condition to a different number?

Can you show how the output would look if we didn't skip even iterations?

Awesome!

Completion rate improved to 2.33

bookChallenge: Stop and Skip in Loops

Swipe to show menu

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.
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:

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.
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

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 8
some-alt