forked from TheAlgorithms/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculate_median.ts
More file actions
25 lines (22 loc) · 786 Bytes
/
Copy pathcalculate_median.ts
File metadata and controls
25 lines (22 loc) · 786 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
* @function calculateMedian
* @description This function will find the median value of an array of numbers.
* @param {number[]} numbers Sorted array of numeric values.
* @return {number} The median of input numbers.
* @see https://en.wikipedia.org/wiki/Median
* @example calculateMedian([1, 2, 4, 5, 8]) = 4
* @example calculateMedian([1, 2, 4, 5]) = 3
*/
export const calculateMedian = (numbers: number[]): number => {
if (numbers.length < 1) {
throw new TypeError('Input array must contain at least one number.')
}
const totalNumbers = numbers.length
if (totalNumbers % 2 === 0) {
const index = totalNumbers / 2
return (numbers[index - 1] + numbers[index]) / 2
} else {
const index = (totalNumbers + 1) / 2
return numbers[index - 1]
}
}