HTML Academy
The while loop, summation
Loops23/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: accumulating misses
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Another project: a dart machine

Appetite comes with eating. The boss accidentally noticed a program that counts the number of points scored before the first miss. Now he sends you greetings and a new task along with them: a driver for a dart machine game.

Machine rules:

  • Each game starts with 0 points.
  • You need to collect 100 or more points to win.
  • You lose the game on your third miss.

We’ll use a while loop and the break and continue statements in this program.

Sometimes programmers create endless loops by mistake after having incorrectly written the stop condition. Such loops can make the browser window in which they are executed “hang”. But if you use endless loops wisely, they can be useful.

We’ll use a while loop to count the points. Before each iteration, its condition will check whether the player still needs more points to win.

In the first step, let’s change the condition so that the game ends when you reach 100 points.

Comments

  • script.js
JavaScript
var score = 0; var total = 0; while (score > -1) { score = muffin.getScore(); total += 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 update the loop to track each throw and log its score to the console.

  1. Add variable victoryPoints, which will store 100, the number of points needed to win.
  2. Replace the loop condition with total < victoryPoints so it runs until the required score is reached.
  3. In the loop, log in the console the result of the shot from the variable score.

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.