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 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”
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 executed. 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 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 the preparatory one. Commands are started here once before the start of the loop. 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 a test. It contains a condition and starts before each new iteration of the loop. The condition here works according to the algorithm you are familiar with. If the condition returns true, the loop completes one more iteration, otherwise the loop stops.

    In the example, we indicated that the loop should work until the variable ibecomes less than 5.

    for (var i = 0; i < 5; i = i + 1) { }
  3. The third part is an additional one, although scientifically speaking it is called the “pattern of change”. The code for the third part starts after each iteration of the loop. That is, after the code is executed from the body of the loop. Usually the counter variable is changed there.

    In our case, we indicated that after each iteration of the loop, the variable i should increase by one.

    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, it is possible to create a counter in a loop without var. This possibility exists for backwards compatibility and came to us from the legacy versions of the language. It worked then, although such code was considered illiterate. But in the new version of JavaScript (ES6), this entry will not work. In the new version, strict mode is enabled by default, which forbids creating variables without var, including the loop counter. Do not forget about var and write the code correctly right away.

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 preparatory part of the loop, create a counter variable: var page = 0,
  3. in the checking part, set the condition for the execution of the loop: page < totalPages,
  4. in the addition part, describe the change in 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−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.