HTML Academy
Printing only even pages
Loops8/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”
Printing driver: all pages
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Printing driver: copy mode

All printing modes are implemented.

It’s time to write a single program that, depending on the set mode, will print the pages as we need them to be printed: multiple copies of one page, all pages of the document in direct and reverse order, only even or only odd pages.

Let’s start with the page copying mode and gradually add other modes. The name of the mode will be stored in the variable mode.

Of course, we will definitely need a loop here. Let’s look at an example to see how this formulation translates into the code: “Add a loop that increases the variable i from zero to 10 inclusive. Value i must increase by one after each iteration”.

It is better to look at this task step-by-step. “Add a loop that increases i from zero to 10 inclusively.” It turns out that the variable that will change its value in the course of the loop will be i. Hence, this is the counter variable. Its value will change from 0 and further. That is, the starting value of the counter is 0. That’s how we should write it:

for (var i = 0;) {
  …
}

Note that we use var to declare the counter. This is the same variable as any other, and you need to declare it with var.

Let’s continue. “…increases the variable i from zero to 10 inclusively”. Hence, the value of the counter (variable i) will grow to 10. Since it says “up to 10 inclusively”, the last value of i, with which the loop will be executed, will be 10. For this value to get into the variable i and for the loop to be executed, use the sign <=.

for (var i = 0; i <= 10;) {
  …
}

“Value i must increase by one after each iteration”. Hence, we must add one to i after each iteration of the loop. We will use the increment.

for (var i = 0; i <= 10; i++) {
  …
}

In the example, we used the standard name i for the loop counter. For page copies, we will count how many copies have already been made and compare this number with the required number. That is, we will increase the number of copies. Therefore, the name copies will work for the counter here.

Comments

  • script.js
JavaScript
var mode = 'pageCopy'; // Printing driver mode var pageNumber = 5; // Copied page number var copyCount = 7; // Number of copies

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 add the copy printing mode.

  1. After declaring the variables, add a condition that checks that mode has the value 'pageCopy'.
  2. Inside this condition, add a loop that increases the variable copies from one to copyCount inclusive. Value of copies must increase by one after each iteration.
  3. Inside the loop, add the command to print the page being copied: muffin.print(pageNumber).

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.