HTML Academy
Dart machine: accumulating misses
Loops25/28
Back to the list of tasks
  • 1. New project: driver development
  • 2. Managing the number of copies
  • 3. Learning the for loop
  • 4. Correcting the loop
  • 5. Printing the pages in reverse order
  • 6. Printing only odd pages
  • 7. Printing only even pages
  • 8. Printing driver: copy mode
  • 9. Printing driver: all pages
  • 10. Printing driver: pages in reverse order
  • 11. Print driver: even and odd pages
  • 12. Print driver: switching between even and odd pages
  • 13. Accumulation in the loop
  • 14. Checks in loops
  • 15. Searching for an even number
  • 16. How much does printing cost?
  • 17. Economy printing
  • 18. Saving ink
  • 19. Summary of “Loops”. Part 1
  • 20. Third program: “Protein shake!”
  • 21. It’s been a “while”
  • 22. The while loop, summation
  • 23. Another project: a dart machine
  • 24. Dart machine: accumulating misses
  • 25. Dart machine: defeat
  • 26. Dart machine: final scoreboard
  • 27. Summary of “Loops”. Part 2
  • 28. Fourth program: “Mad Dryer”
Dart machine: final scoreboard
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Dart machine: defeat

It’s time to break out of this endless series of wins!

The program already knows how to accumulate misses in the misses variable. All we have left to do is teach it how to stop the loop after reaching three misses. But where in the loop should this condition be checked? In the beginning, the middle or the end?

Here is the current order of the commands in the loop:

  1. We get the result of the shot.
  2. Check that the shot result is less than zero. If this is the case, then go to step three, otherwise go to step four.
  3. Record the miss in the console and increase the miss counter by one.
  4. For a hit, add the score to total and log the throw’s result to the console.

We need the following:

  • A check for the total number of misses must be completed at every iteration of the loop.
  • When the three-miss mark is reached, the game must immediately stop.

We’ll check the miss count at the beginning of the loop, before taking another throw or getting its score.

We need a command that will interrupt the execution of the loop.

In JavaScript, the break statement stops the loop. Add a check for the miss count at the start of the loop body, and use break when it reaches three.

Use continue carefully: it skips the remaining code in the current iteration, which can make the loop’s flow harder to follow.

  • In a while loop, continue skips the remaining body code and checks the condition again.
  • In a for loop, continue skips the remaining body code, runs the update, then checks the condition before the next iteration.

Comments

  • script.js
JavaScript
var score = 0; var total = 0; var victoryPoints = 100; var misses = 0; while (total < victoryPoints) { score = muffin.getScore(); if (score < 0) { console.log('You missed!'); misses++; } else { total += score; console.log('Result of the shot: ' + score); } } console.log(total);

What didn’t you like in this task?

Thanks! We’ll fix everything at once!

Console

The code has changed, click “Run” or turn autorun on.

Result

Goalscompleted

Let’s make it so that the loop stops if the number of misses goes up to three.

  1. Add condition if (misses >= 3) { } to the beginning of the loop body.
  2. Inside this if block, add the break statement.

Cookies ∙ Privacy ∙ License Agreement ∙ About ∙ Contacts ∙ © HTML Academy OÜ, 2019−2026

VISAMastercard

Log in

or

Forgot your password?

Sign up

Sign up

or
Log in

Restore access

Have you forgotten your password or lost access to your profile? Enter your email connected to your profile and we will send you a link to restore access.

Forgot to connect your email to the profile? Email us and we’ll help.