Course Content
Introduction to JavaScript
Introduction to JavaScript
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.
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"); }; };
The output should be:
python
Include the counter variable in the firstΒ
console.log()
Β statement.Use theΒ
break
Β statement inside theΒif (i >= 5)
Β block to stop the loop.Output the stringΒ
"Stop"
Β to the console before theΒbreak
Β statement.Use theΒ
continue
Β statement inside theΒif (i % 2 == 0)
Β block to skip even iterations.Output the stringΒ
"Skip"
Β to the console before theΒcontinue
Β statement.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"); } }
Thanks for your feedback!