HTML Academy
It’s been a “while”
Loops22/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 evens and odds
  • 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”
Another project: a dart machine
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

The while loop, summation

In the previous step, you wrote the main loop. The most important thing is that this loop can stop!

Remember the condition:

while (score > -1) { }

In order for the loop to stop, the condition must at some point become false. For this to happen, the variable score must change within the loop. That’s what we made sure would happen when we started to record the result of the next shot at each iteration.

If the loop stop condition does not work, the loop cannot stop. This is an endless loop, one of the favorite errors of programmers. By the way, the loop for can also be made infinite.

Part of the logic of the program is ready: we get the results of all the shots and stop the game when a miss occurs. All we have left to do is calculate the sum of points.

To do this, before the loop, declare variable total, and in each iteration, we will add to it the points received for the shot.

And, at last, we’ll log the result of the game in the console.

Comments

  • script.js
JavaScript
var score = muffin.getScore(); while (score > -1) { console.log(score); score = muffin.getScore(); }

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
  1. On the next line after the variable score, declare a variable total, which equals 0.
  2. Inside the loop, instead of logging the variable score in the console, increase the value of total to score using the operator +=.
  3. After the loop, log the value of the variable total in the console.

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

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.