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 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: 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 starting value of the counter is 10 and it will decrease to 1 inclusively, the “more or equal to” sign will work for us. We will compare with 1 since this is the value after which the loop should stop.

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

Note that the condition can also be written slightly differently. If we need to take into account the value 1, but end the loop with a value less than 1, we can use the “more than” sign. We should only compare it with 0. Then one will be used precisely as the counter value, and for 0, the loop will end its work.

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

There are two options to write a condition in such cases, and both are true. You can use any.

“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. In this mode, we will count the pages in reverse order and wait for the last page to print, in our case the first one, since we count from the end. We already had the page counter, that’s why we will use the reversePage since we count the pages in reverse order.

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−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.