HTML Academy
Array length
Arrays9/30
Back to the list of tasks
  • 1. Little Muffin and Big Data
  • 2. Average value
  • 3. Finishing the traffic analyzer
  • 4. New data
  • 5. A lot of data? An array!
  • 6. Reading from an array using index
  • 7. Variable as index
  • 8. Array length
  • 9. Let’s hit arrays with loops!
  • 10. Summation in the loop
  • 11. Finishing refactoring
  • 12. One small analytical investigation
  • 13. Writing to an array by index
  • 14. The murderer is a butler!
  • 15. Summary of “Arrays”. Part 1
  • 16. Fifth program: Beginner decryptor
  • 17. Vague suspicions
  • 18. Swapping the elements
  • 19. Looking for the minimum element
  • 20. The minimum element is found!
  • 21. Starting sorting
  • 22. Continue sorting
  • 23. Finish sorting
  • 24. Testing the sorting
  • 25. Median of an odd number of elements
  • 26. Median of an even number of elements
  • 27. Green light
  • 28. The murderer is the butler, again!
  • 29. Summary of “Arrays”. Part 2
  • 30. Sixth program: Long jump records
Summation in the loop
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Let’s hit arrays with loops!

Loops and arrays are closely related. For many operations with arrays, for example, counting sums of elements, loops are used. Now you are ready to use a loop to bypass the array.

Loop for will be the best for this task. The loop variable will be used as an index of array elements. Therefore, let’s assign it a zero value and increase it by one at each iteration until its value becomes equal to the index of the last element.

The traditional name for the loop variable i is an abbreviation of index, that is, an index or a serial number. This is also one of the agreements among programmers, which allows us to shorten the code.

And one more style-related detail. What is the best way to write the exit condition for the loop? There are two options. Let’s analyze then using an example of an array of three elements:

// First option: i < usersByDay.length
// usersByDay.length == 3

Preparation: i = 0
1 iteration: i = 0; 0 < 3? yes! first iteration actions; i = 1
2 iteration: i = 1; 1 < 3? yes! second iteration actions; i = 2
3 iteration: i = 2; 2 < 3? yes! third iteration actions; i = 3
4 iteration: i = 3; 3 < 3? no! Finish the loop!
// Second option: i <= usersByDay.length - 1
// usersByDay.length - 1 == 2

Preparation: i = 0
1 iteration: i = 0; 0 <= 2? yes! first iteration actions; i = 1
2 iteration: i = 1; 1 <= 2? yes! second iteration actions; i = 2
3 iteration: i = 2; 2 <= 2? yes! third iteration actions; i = 3
4 iteration: i = 3; 3 <= 2? no! Finishing the loop!

Both exit conditions options, i < usersByDay.length and i <= usersByDay.length - 1, work the same way. But for now we will use the second option, subtracting one. It will remind us of the unusual numbering of array elements that starts from zero.

Comments

  • script.js
JavaScript
var expectedUsers = 1000; var usersByDay = [812, 1360, 657, 1247]; // Drawing the traffic graph muffin.plot(usersByDay, expectedUsers); // Calculating the average traffic value var averageUsers = 0; console.log('Average traffic: ' + averageUsers); if (averageUsers > expectedUsers) { console.log('Traffic is amazing. Keep up the good work!'); } else { console.log('Traffic is so-so. You need to try harder!'); }

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. After the command muffin.plot(), add a loop that increases variable i from zero to usersByDay.length - 1 inclusively. Value of i must increase by one after each iteration.
  2. Inside the loop, log in the console a value of usersByDay[i].

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.