Challenge: Stop and Skip in Loops
Task
Implement a loop that skips even iterations and stops on the 5th iteration. Here are the instructions:
- The loop should stop at the 5th iteration.
- For each iteration, output the iteration number to the console.
- If the loop skips an iteration, output the word
"Skip"
to the console. - If the loop stops, output the word
"Stop"
to the console.
123456789101112131415for (let i = 1; i <= 10; i++) { console.log("Iteration", ___); if (i >= 5) { console.log("___"); ___; }; if (i % 2 == 0) { console.log("___"); ___; } else { console.log("Successful"); }; };
The output should be:
Iteration 1
Successful
Iteration 2
Skip
Iteration 3
Successful
Iteration 4
Skip
Iteration 5
Stop
- Include the counter variable in the first
console.log()
statement. - Use the
break
statement inside theif (i >= 5)
block to stop the loop. - Output the string
"Stop"
to the console before thebreak
statement. - Use the
continue
statement inside theif (i % 2 == 0)
block to skip even iterations. - Output the string
"Skip"
to the console before thecontinue
statement. - For other cases, output
"Successful"
to the console.
123456789101112131415for (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"); } }
Everything was clear?
Thanks for your feedback!
Section 5. Chapter 8
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 2.33
Challenge: 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:
- The loop should stop at the 5th iteration.
- For each iteration, output the iteration number to the console.
- If the loop skips an iteration, output the word
"Skip"
to the console. - If the loop stops, output the word
"Stop"
to the console.
123456789101112131415for (let i = 1; i <= 10; i++) { console.log("Iteration", ___); if (i >= 5) { console.log("___"); ___; }; if (i % 2 == 0) { console.log("___"); ___; } else { console.log("Successful"); }; };
The output should be:
Iteration 1
Successful
Iteration 2
Skip
Iteration 3
Successful
Iteration 4
Skip
Iteration 5
Stop
- Include the counter variable in the first
console.log()
statement. - Use the
break
statement inside theif (i >= 5)
block to stop the loop. - Output the string
"Stop"
to the console before thebreak
statement. - Use the
continue
statement inside theif (i % 2 == 0)
block to skip even iterations. - Output the string
"Skip"
to the console before thecontinue
statement. - For other cases, output
"Successful"
to the console.
123456789101112131415for (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"); } }
Everything was clear?
Thanks for your feedback!
Section 5. Chapter 8