HTML Academy
Summation in the loop
Arrays11/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 an array element by index
  • 7. Using a variable as an 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
One small analytical investigation
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Finishing refactoring

In the first step of refactoring, we got rid of individual variables and intentionally turned off the calculation of the average value by setting the variable averageUsers to zero. It’s time to restore the calculation using an array.

Remember that the average value is the sum of all the elements divided by their number. We have already calculated the sum in the loop, and the number of elements can be obtained using usersByDay.length.

Refactoring is complete, and here is the cherry on the cake! Now the program has become much more flexible and allows you to easily analyze any amount of data (for a week, a month, or even a year). To do this, you just need to change the values ​​inside the array usersByDay.

Finally, complete Muffin’s task. Analyze traffic for the last month. To do this, copy this data into the array:

817, 1370, 752, 1247, 681, 1120, 915, 1281, 875, 1341, 757, 610, 812, 1170, 769, 1261, 845, 1289, 515, 1247, 845, 1311, 741, 1239, 812, 638, 877, 1242, 1159, 1372

Now the program works with arrays of different lengths. But there is one nuance. If you run the program on an empty array, then the average traffic value will be NaN (stands for not a number). It couldn’t be any other way, because in the formula for calculating the average value, zero will be the divisor and the dividend at the same time, and 0 / 0 gives us NaN. It’s quite logical if you think about it: we cannot know the average value of non-existent values, so the calculation provides an average for non-empty arrays; an empty array would need separate handling.

Comments

  • script.js
JavaScript
var expectedUsers = 1000; var usersByDay = [812, 1360, 657, 1247]; // Drawing the traffic graph muffin.plot(usersByDay, expectedUsers); // Summing traffic var totalUsers = 0; for (var i = 0; i <= usersByDay.length - 1; i++) { totalUsers += usersByDay[i]; } console.log(totalUsers); // 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

Let’s finish program refactoring.

  1. Remove logging of the totalUsers variable in the console.
  2. Set averageUsers to totalUsers / usersByDay.length instead of zero.
  3. Replace the values in usersByDay with the figures from Muffin. Copy them from the theory and paste them between the square brackets.

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.