HTML Academy
Searching for an even number
Loops16/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”
Economy printing
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

How much does printing cost?

Back to the printer. We wrote a program that knows how to print pages, but we missed one detail: ink is used to print each page. Our printer uses 70 mg of ink per page. It would be nice to monitor the toner consumption and know how much it costs to print one document.

To solve this task, we’ll use the accumulation of values ​​in the loop. We’ll set up an external variable, where we’ll add 70 mg (consumption per page) when printing each page of the document. But first you need to name this variable.

You do not have to invent complex names for variables, their purpose should be made clear by the name. Therefore, we determine what should be stored in the variable, for example, total ink consumption for all pages, and then translate it into English. We could call the variable consumptionColorForAllPages or consumptionTonerForAllPages, but this is redundant. We only solve the toner consumption task, we do not have another code, and that is why we can shorten the name and remove the indication of what we consider consumption. This is already clear, since we have no other task. A ForAllPages too long. If we ignore the details, we calculate the total ink consumption. The English word “total” describes our purpose pretty well, and that is why we can call the variable consumptionTotal. This name is concise and at the same time it explains the value of the variable.

Let’s repeat the procedure for the name of the variable that will store ink consumption per page (70 mg). Let’s call it consumptionPerPage.

Let’s count toner consumption for printing all pages. We’ll make our calculations in print mode for regular documents.

Comments

  • script.js
JavaScript
var totalPages = 6; // Total number of pages for (var page = 1; page <= totalPages; page++) { muffin.print(page); }

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. After the variable totalPages, declare a variable consumptionTotal equal to 0.
  2. After it, create a variable consumptionPerPage. It equals 70.
  3. Inside the loop, after initiating the muffin.print() command, increase the value of consumptionTotal variable by consumptionPerPage with the help of the += operator.
  4. Below, in the loop, log the consumptionTotal value 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.