HTML Academy
New project: driver development
Loops2/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”
Learning the for loop
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Managing the number of copies

The driver can make three copies of the given page. Now you need to train it to make a different number of copies depending on the situation. You know the conditions, and that’s why you could write the program like this:

// Variable stores the necessary number of copies
var count = 3;

if (count === 2) {
  muffin.print(page);
  muffin.print(page);
}

if (count === 3) {
  muffin.print(page);
  muffin.print(page);
  muffin.print(page);
}

In general, the approach is working, but no one writes programs like that. Imagine a program that can make one hundred copies of one page.

To make execution of these repeated commands convenient, there are loops in programming languages. For example, the loop for:

for (var i = 0; i < count; i = i + 1) {
  // repeated commands
}

If we write the loop for like this, the actions inside curly brackets will be executed count times. We will postpone careful consideration of the contents of for until the next task, but for now we’ll see how it works.

Modify the driver so that you can control the number of copies with the variable count.

By the way, in the “Conditions” chapter, we looked into strict (===) and approximate (==) equality. Why is it that strict equality is used in the example above?

Because such a comparison helps to avoid errors. For example, this comparison will work if we use approximate equality:

if ('003' == 3) {
  // …
}

The same check using === will not work, because line '003' is not the same as number 3. In our program, the number of copies is written with a number and in all comparisons, we expect a number. Therefore, we use strict equality to make sure that inappropriate values do not slip into our program.

Using strict equality is good practice. Use it and nothing else in all cases where possible.

Comments

  • script.js
JavaScript
1
2
var page = 7;
var count = 3;
 
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

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 0 out of 3

Replace command duplication with the for loop.

  1. Delete the muffin.print commands and replace them with the loop: for (var i = 0; i < count; i = i + 1) { }.
  2. Inside the loop, add the command muffin.print(page).
  3. Change the value of the variable count to 5 and make sure that the number of printed copies has changed.

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.