HTML Academy
One small analytical investigation
Arrays13/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
The murderer is a butler!
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Writing to an array by index

It seems to work! There are many dip days. But so far nothing is clear.

In order to clarify the picture, I propose to collect data about the dips in the new array and log it on the second graph. To do this, you will have to write data to an array. Writing to an array is done in the same way as reading: by accessing an element using square brackets:

var numbers = [];
var index = 1;

numbers[0] = 1;
numbers[index] = 2;

// Will log [1,2]
console.log(numbers);

Note that if there is no element in the array under the number under which we write it, then this element will be created. Before executing the code in the array, there was neither zero nor the first element, but after we wrote down the values ​​in these positions, the elements were added to the array.

In order to maintain consistency with the first graph, we will form an array with dip days in the same loop, where we summarize traffic. If traffic on the i-th day is good, we will write zero dips in the i-th element of the array, if traffic is bad, we will write the dip value there.

To calculate dip value, we will subtract the actual traffic value from the expected value. For example:

  1. Traffic on the fifth day was 681 people.
  2. Difference between expected and actual traffic: 1000 - 681 = 319.
  3. It means 319 people is the traffic dip value on the fifth day.

Comments

  • script.js
JavaScript
var expectedUsers = 1000; var usersByDay = [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]; // Drawing the traffic graph muffin.plot(usersByDay, expectedUsers); // Summarizing traffic and analyzing the dips var totalUsers = 0; var minUsers = expectedUsers - 100; for (var i = 0; i <= usersByDay.length - 1; i++) { totalUsers += usersByDay[i]; if (usersByDay[i] < minUsers) { console.log(usersByDay[i]); } } // Calculating the average traffic value var averageUsers = totalUsers / usersByDay.length; 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 variable minUsers, add empty array badDays.
  2. If checking for a day dip of traffic is successful, write in the i-tharray element badDays value expectedUsers - usersByDay[i]. Delete current value console log.
  3. If the condition is not fulfilled, write zero in the i-th array element badDays.
  4. After the loop, add another muffin.plot() command with parameters badDays and 100.

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.