HTML Academy
Green light
Arrays28/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
Summary of “Arrays”. Part 2
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

The murderer is the butler, again!

Well, it’s already clear that something is wrong here. After all, the average traffic is 1,032, and the median value is 89 people per day. And this is already below the expected value of a thousand people.

There is one problem here. Muffin is an old-school cat and he loves percentage. To convince him that there’s some fraud going on, it is necessary to prove that the average and median differ at least by 10 percent.

In our case, it is very suspicious when the median is below average. All we have left to do is to understand how to calculate the percentage.

To do this, we need to divide the median value by the average value. For example, if the median is 80 and the median is 100, then:

// Median is 80% of the average
80 / 100 = 0.8

Let’s reformulate Muffin’s task: if the median is less than 0.9 from the average, something fishy is going on.

All we have left to do is to finish writing the check and log the recommendations in the console.

Comments

  • script.js
JavaScript
var expectedUsers = 1000; var usersByDay = [817, 581, 1370, 752, 1247, 681, 1120, 915, 875, 1341, 757, 610, 812, 741, 1139, 812, 638, 877, 1242, 1159, 1372, 1170, 845, 1289, 515, 1247, 769, 1261, 2805, 1201]; // Drawing the traffic graph muffin.plot(usersByDay, expectedUsers); // Summarizing traffic var totalUsers = 0; for (var i = 0; i <= usersByDay.length - 1; i++) { totalUsers += 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!'); } // Sorting the array for (var i = 0; i <= usersByDay.length - 2; i++) { var minValue = usersByDay[i]; for (var j = i + 1; j <= usersByDay.length - 1; j++) { if (usersByDay[j] < minValue) { minValue = usersByDay[j]; var swap = usersByDay[i]; usersByDay[i] = minValue; usersByDay[j] = swap; } } } // Calculating median if (usersByDay.length % 2 !== 0) { var medianIndex = (usersByDay.length - 1) / 2; var median = usersByDay[medianIndex]; } else { var leftIndex = usersByDay.length / 2 - 1; var rightIndex = usersByDay.length / 2; var median = (usersByDay[leftIndex] + usersByDay[rightIndex]) / 2; } console.log('Median traffic: ' + median);

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. At the end of the program, add a check to make sure that median / averageUsers < 0.9.
  2. If the condition is met, log the following message in the console 'Something is fishy here!'.
  3. Otherwise, log this message in the console 'There are no suspicions of fraud!'.

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.