HTML Academy
Third program: “Protein shake!”
Loops21/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”
The while loop, summation
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

It’s been a “while”

Great! You figured the for loop out. It’s convenient to use when the number of repetitions is known in advance, even before the start of the loop. For example, when you know the number of pages in the document or the number of days.

But there are situations where the number of repetitions is not known beforehand. For example, biathlon: shoot at targets until you hit every target.

In these cases, a while loop is useful: it repeats the actions while its condition is true.

while (condition) {
  actions
}

The actions specified in the loop will be executed again and again until the condition returns false.

Let’s practice using a while loop before the boss gives us yet another job to work on.

When on break from work, the boss likes to play darts. He is trying to set a record: to score the maximum number of points in a series of throws without a miss. As soon as he misses, the series ends (and judging by the holes in the wall, he misses quite often).

We need a simple program to track these attempts. The muffin.getScore() command gives us the score for the next throw: a number from 1 to 20 for a hit, or -1 for a miss.

Comments

  • script.js
JavaScript
var score;

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

Write the main program loop with muffin.getScore().

  1. Assign the result of muffin.getScore() to the score variable.
  2. Add loop while (score > -1) { } under the variable. “Timeout error” might be logged on this and the next step. This is normal.
  3. Inside the loop, log the variable score in the console.
  4. After logging score inside the loop, assign the result of muffin.getScore() to score again.

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.