HTML Academy
Testing the sorting
Arrays25/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
Median of an even number of elements
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Median of an odd number of elements

The sorting program is ready and tested. Now let’s proceed to calculation of the median. For a sorted array with an odd number of elements, the median is the value of the middle element.

Median

The median is less affected by extreme values than the average:

// Average: 3
[1, 2, 3, 4, 5]
// Median: 3
[1, 2, 3, 4, 5]

// Average: 12
[1, 2, 3, 4, 50]
// Median: 3
[1, 2, 3, 4, 50]

It is easy to calculate a median for an array with an odd number of elements. To do this, you need to correctly calculate the middle element’s index. Let’s try to derive the formula:

// Array length 3, middle element index 1
[1, 2, 3]

// Array length 5, middle element index 2
[1, 2, 3, 4, 5]

// Array length 7, middle element index 3
[1, 2, 3, 4, 5, 6, 7]

Let’s subtract one from the array length and divide it by two to get the index.

Muffin can send data arrays of different lengths, so you’ll have to check whether the number of elements is odd or even and, depending on the result, find the median. To check this, let’s use the operator we are already familiar with: %.

Comments

  • script.js
JavaScript
var usersByDay = [1, 2, 3, 4, 5]; console.log(usersByDay);

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 for an odd number of elements in the array: usersByDay.length % 2 !== 0.
  2. If the condition is true, declare medianIndex with the value (usersByDay.length - 1) / 2.
  3. Log this variable in the console.
  4. In the same if block, declare median with the value usersByDay[medianIndex].
  5. Log it in the console as well.

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.