HTML Academy
Printing driver: all pages
Loops10/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”
Print driver: even and odd pages
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Printing driver: pages in reverse order

Now we’ll print the pages in reverse order.

The mode will be called 'reverse'. You need to add a condition and a separate loop.

How to write loops for similar conditions: “Add a loop that decreases the value of the variable i from 10 to 1 inclusively. Decrease the value of i by one at each iteration”? As usual, we will look into it step-by-step.

”…a loop that decreases the value of variable i from 10 up to 1 inclusively.” The counter here is i, and its starting value is 10. Writing it down.

for (var i = 10;) {
  …
}

”…a loop that decreases the value of the variable i from 10 to 1 inclusively.” This means that the loop will complete its work when i becomes equal to 0. For i = 1, the loop will execute the next iteration. Since the counter starts at 10 and decreases to 1 inclusive, we use “greater than or equal to”. Comparing with 1 includes that final value before the loop stops.

for (var i = 10; i >= 1;) {
  …
}

We can also write the condition another way. To include 1 but stop when the counter reaches 0, use “greater than” and compare with 0. This lets the loop run with i equal to 1, then stop at the next check, when i is 0.

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

Both conditions work here. You can use either one.

“Decrease the value of i by one at each iteration.” Here we have the value change by one that we are used to. Only now we do not increase the value of the counter, but decrease it, and that’s why we use a decrement.

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

Back to the printer. This mode counts down from the last page to page 1, which is printed last. We’ve already used page for the forward-printing counter. Let’s name this counter reversePage to make its purpose clear.

Comments

  • script.js
JavaScript
var mode = 'document'; // Printing driver mode var pageNumber = 5; // Copied page number var copyCount = 7; // Number of copies var totalPages = 6; // Pages in the document 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); } }

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 printing mode of the document pages in the reverse order.

  1. At the end of the program, add a check that compares mode with the value of 'reverse'.
  2. Inside this check, add a loop that decreases the value of reversePage from totalPages to 1 inclusively. Decrease the value of reversePage by one at each iteration.
  3. Inside the loop, add the print command for the current page reversePage.
  4. Change the value of the mode variable to 'reverse'.

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.