HTML Academy
Learning the for loop
Loops4/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”
Printing the pages in reverse order
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Correcting the loop

Something went wrong, and instead of printing pages one through seven, the driver prints a blank page and then pages one through six. Let’s look at the sequence in which the parts of the for structure are implemented using this example:

for (var i = 0; i < 5; i = i + 1) {
  console.log(i);
}
  1. First, var i = 0; works. i variable will be created, which is equal to 0.
  2. Then the i < 5; test will be performed. Since i is now equal to 0 and this value is less than 5, the condition returns true. This means that the loop can continue working and go to execution of the code from the body of the loop.
  3. The code will be executed from the body of the loop. In our case, the console will log 0, the current value of the i variable.
  4. Then i = i + 1 will be executed. The variable i becomes equal to 1.
  5. Then i < 5; will be tested. It will return true once again, since 1 is less than 5.
  6. The body of the loop is executed again, then the value i will increase by one, and so on in a loop until the condition i < 5; doesn’t return false. Then the loop will finish its work.

Now let’s look at the example from the previous task. Why was the blank page printed first?

On the first go-round or, scientifically speaking, during the first iteration of the loop, variable page still equals zero. And it increases to one already after the first iteration. If you change the initial value of the variable page from 0 to 1, then the problem disappears.

Why are there six pages, and not seven? Let’s take a look at the loop step-by-step:

Preparation: totalPages = 7; page = 0
1 iteration: page = 0; 0 < 7? yes! Printing page 0; page = 1
2 iteration: page = 1; 1 < 7? yes! Printing page 1; page = 2
3 iteration: page = 2; 2 < 7? yes! Printing page 2; page = 3
4 iteration: page = 3; 3 < 7? yes! Printing page 3; page = 4
5 iteration: page = 4; 4 < 7? yes! Printing page 4; page = 5
6 iteration: page = 5; 5 < 7? yes! Printing page 5; page = 6
7 iteration: page = 6; 6 < 7? yes! Printing page 6; page = 7
8 iteration: page = 7; 7 < 7? no! Finish the loop!

To get the seventh page, you need to change the comparison from “less than” to “less than or equals to”. In this case, the check on the eighth iteration of the loop will work and the seventh page will be printed.

8 iteration: page = 7; 7 <= 7? yes! Printing page 7; page = 8
9 iteration: page = 8; 8 <= 7? yes! Finish the loop!

To summarize: to make everything print as it should, we need to start counting from page number 1 and use the <= sign in condition to include the last page in the loop.

Comments

  • script.js
JavaScript
var totalPages = 7; for (var page = 0; page < totalPages; page = page + 1) { 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 correct the print mode for all pages.

  1. Replace the initial value of the variable page to 1.
  2. Replace “less than” with “less than or equals to” in the loop condition.
  3. To make sure that everything works, set the variable totalPages to value 5. You should get five pages, starting with the first.

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.