HTML Academy
Let’s hit arrays with loops!
Arrays10/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
Finishing refactoring
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Summation in the loop

In a loop, you can log array elements in the console, as well as perform some operations with them. For example, summarize. You already did this in the “Loops” chapter.

To do this, you must enter a variable with a zero value before the loop. Then at each iteration add to it the value of the next element of the array. As a result, after the loop, the variable will be the sum of all the elements.

Our task is to find the average value. And for this, you first need to calculate the sum of all the elements.

Let’s rewrite the loop so that it doesn’t log the values ​​of the elements in the console, but consistently stacks them together and writes them into one variable.

Comments

  • script.js
JavaScript
var expectedUsers = 1000; var usersByDay = [812, 1360, 657, 1247]; // Drawing the traffic graph muffin.plot(usersByDay, expectedUsers); for (var i = 0; i <= usersByDay.length - 1; i++) { console.log(usersByDay[i]); } // 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. Before the loop, add a variable totalUsers with a zero value.
  2. Inside the loop, remove console log and increase the value of the variable totalUsers by usersByDay[i].
  3. Inside the loop, log the variable totalUsers 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.