HTML Academy
Managing the number of copies
Loops3/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”
Correcting the loop
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Learning the for loop

Excellent! The first part of the driver is complete. The second step: printing all pages of the document.

This task also has many repeated actions, and we therefore won’t be able to solve it without loops. Let’s look at the for loop in more detail:

for (var i = 0; i < 5; i = i + 1) {
  // repeated commands or the “loop body”
}

How does the for loop work? It executes the actions from the loop body again and again, as long as the condition returns true. We are about to look into what kind of condition it is and how we can write loops correctly.

Loop control code must be in parentheses. It consists of three parts, separated by ;. The meaning of each part is this:

  1. The first part is initialization. This code runs once, before the loop starts. Usually, the initial value for the counter variable is set here. We can say that the first part is the initial setting for the loop.

    In the example below, we created the counter variable i and set to it the initial value 0. The variable will be equal to this value when the loop begins to work.

    Note that in the loop, we create a counter variable with var, as in the case of any other variable. Traditionally, such a variable is called i (from the word index), but it can have any other name. For example, if a loop counts days, the counter variable may be called day, and if it counts pages of the document, then it can be called page.

    for (var i = 0; i < 5; i = i + 1) { }
  2. The second part is the condition. It is checked before each iteration, just like the conditions you already know. If it is true, the loop runs another iteration; otherwise, it stops.

    In this example, the loop continues while i is less than 5.

    for (var i = 0; i < 5; i = i + 1) { }
  3. The third part is the update. It runs after each iteration, once the loop body has finished. This is usually where the counter changes.

    In our example, i increases by one after each iteration.

    for (var i = 0; i < 5; i = i + 1) { }

We have analyzed how the for loop is structured in theory. Now let’s write a program for printing all pages. The number of pages is stored in the variable totalPages.

By the way, declare each new loop counter with var. Assigning to an undeclared name can create an accidental global in non-strict code; in strict mode it causes an error. ES6 does not enable strict mode for every script. An existing variable can be reused without declaring it again.

Comments

  • script.js
JavaScript
var totalPages = 7;

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 log the pages of the document in the loop.

  1. Under the variable declaration, add an empty loop to the code for (;;) { }.
  2. In the initialization part of the loop, create a counter variable: var page = 0,
  3. in the condition part, set the test for continuing the loop: page < totalPages,
  4. in the update part, increase the counter: page = page + 1.
  5. And in the body of the loop, add the page print command: muffin.print(page).

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.