HTML Academy
Printing driver: pages in reverse order
Loops11/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”
Print driver: switching evens and odds
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Print driver: even and odd pages

Now let’s add a mode that skips every second page.

The mode will be activated at the 'alternate' value of the mode variable.

To choose whether to print even or odd pages, we will use the variable startPage: if it equals 1, odd pages are printed, and if it equals 2 even pages are printed.

Since we have an alternative type of printing, pages are printed in an unusual way. The counter can be called approximately the same as the printing mode alternatePage.

Note that the increment is not going to be useful here. The counter will increase by two, that’s why you should use an entry of the type counter = counter + 2 or the shorter version counter += 2.

Now we’ll add a new printing mode, and in the next task, we will experiment with this mode and switch between printing even and odd pages.

Comments

  • script.js
JavaScript
var mode = 'reverse'; // Printing driver mode var pageNumber = 5; // Copied page number var copyCount = 7; // Number of copies var totalPages = 6; // Pages in the document var startPage = 1; // Starting page if (mode === 'pageCopy') { for (var copies = 1; copies <= copyCount; copies++) { muffin.print(pageNumber); } } if (mode === 'document') { for (var page = 1; page <= totalPages; page++) { muffin.print(page); } } if (mode === 'reverse') { for (var reversePage = totalPages; reversePage >= 1; reversePage--) { muffin.print(reversePage); } }

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 a mode for printing even and odd pages of the document.

  1. Add a check for the 'alternate' mode at the end of the program.
  2. Inside this check, add a loop where the value of alternatePage variable increases by 2 from the value startPage to totalPages inclusively.
  3. Inside the loop, add the page printing command alternatePage.

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.