HTML Academy
Median of an odd number of elements
Arrays26/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
Green light
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Median of an even number of elements

  • Well done! Take a patty from the shelf.
  • But there are two of them there.
  • Take the one in the middle.

Approximately the same situation occurs with calculating the median when an even number of elements are stored in the array. But unlike the situation with patties, we found the solution for the median. If the number of elements is even, the median is calculated as the average of the two elements: the left one and the right one from the middle.

// Median: 3
[0, 1, 2, 4, 50, 100]

Let’s once again derive the formulas of the indices of the two elements: the left one and the right one from the middle.

// Length 1, left index 1, right index 2
[1, 2, 3, 4]

// Length 6, left index 2, right index 3
[1, 2, 3, 4, 5, 6]

// Length 8, left index 3, right index 4
[1, 2, 3, 4, 5, 6, 7, 8]

Divide the array length by two and subtract one: and we got the left index. Divide array length by two: and we got the right index.

Let’s add calculating the median for an even number of elements in the array to the alternative branch of the condition.

Please note, that this median search algorithm will not work for empty arrays and the result will be NaN. It’s because value undefined sneaks into the calculations, and this value returns NaN in any mathematical operations.

Comments

  • script.js
JavaScript
var usersByDay = [1, 2, 3, 4, 5, 6]; console.log(usersByDay); if (usersByDay.length % 2 !== 0) { var medianIndex = (usersByDay.length - 1) / 2; console.log(medianIndex); var median = usersByDay[medianIndex]; console.log(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. In the alternative branch of the condition, add the variable leftIndex with the value usersByDay.length / 2 - 1.
  2. And variable rightIndex with value usersByDay.length / 2.
  3. Log both variables in the console.
  4. In the same condition branch, add variable median with value (usersByDay[leftIndex] + usersByDay[rightIndex]) / 2.
  5. Log it in the console as well.

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.