HTML Academy
Print driver: switching between even and odd pages
Loops13/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”
Checks in loops
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Accumulation in the loop

Inside the loops, in addition to printing pages, you can also use standard mathematical operations. For example, addition:

for (var i = 1; i <= 5; i++) {
  console.log(2 + 2);
}

The result of the program will be the following:

LOG: 4 (number)
LOG: 4 (number)
LOG: 4 (number)
LOG: 4 (number)
LOG: 4 (number)

Number 4 will be logged in the console 5 times, because this is how many iterations this loop has. That is, at each iteration, twos will be added and logged in the console. But if at each iteration we need to get a new, increased, number, we must act differently. We need to create one more variable before the loop, which will store the sum:

var sum = 0;

for (var i = 1; i <= 5; i++) {
  sum += 2;
  console.log(sum);
}

The program will log:

LOG: 2 (number)
LOG: 4 (number)
LOG: 6 (number)
LOG: 8 (number)
LOG: 10 (number)

Each iteration adds 2 to sum, building up a running total. We declare and initialize sum before the loop, rather than inside its body. This is important: its value then carries over between iterations instead of being reset to zero each time.

This operation is called accumulation of a value in the loop.

Let’s practice accumulating values ​​and calculate the sum of numbers from 1 to 10. We will log intermediate results in the console to monitor the changes in the sum.

Comments

  • script.js
JavaScript

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
  1. At the beginning of the code, create variable sum, which equals 0.
  2. Below the variable declaration, add a for loop that counts i from 1 to 10 inclusive.
  3. Inside the loop, at each iteration, increase the sum by the counter value sum += i;.
  4. In the body of the loop, after increasing sum, log 'i: ' + i in the console.
  5. Then log 'sum: ' + sum in the console.

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.