HTML Academy
Checks in loops
Loops15/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”
How much does printing cost?
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Searching for an even number

Checks in loops are very convenient. They allow you to do checks at each iteration of the loop.

For example, we will check whether the number i is now even in the loop from previous tasks. If the number is even, we will add 2 to the sum, and if the number is odd, we will add 1.

How can I check if the number is even? Here the operator % can help. It is called the remainder in division and, as the name implies, returns the remainder in division.

10 % 5;  // Returns 0
12 % 5;  // Returns 2
7 % 3;   // Returns 1
5.5 % 2; // Returns 1.5

How does this help in determining an even or odd number? An even number is divided by 2 without remainder. Therefore, if the division i% 2 returns 0 the number is even, otherwise the number is odd.

Let’s write a check using % in our loop.

By the way, the value returned by the operator % is the same as the remainder in division in arithmetic. This division is easier to understand on an everyday example. Imagine that you have 13 chocolates, and there are 4 people (including you). How to divide the chocolates among everyone so as not to offend anyone? It’s not possible to divide the chocolates equally, because 13 cannot be divided by 4 without fractional parts. But 12 can be equally divided by 4. You can give 3 chocolates to everyone. Then you will give out 12 chocolates and there will still be one left, because initially there were 13 chocolates. This left-over chocolate is the remainder in division.

Comments

  • script.js
JavaScript
var sum = 0; for (var i = 1; i <= 10; i++) { console.log('i: ' + i); // Add a check here console.log('sum: ' + sum); }

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. Inside the loop, add a check to make sure that the counter is even (i % 2 === 0).
  2. If the check is executed, increase the variable sum by 2.
  3. Inside the condition, after increasing the sum, log the 'even number' string in the console.
  4. After the check is complete, add the else branch and increase the sum inside it by 1.
  5. Inside the else, after increasing the sum, log the 'odd number' line in the console.

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.