Commit | Line | Data |
---|---|---|
6e9d10db JB |
1 | /** |
2 | * An intentional empty function. | |
3 | */ | |
4f3c3d89 | 4 | export const EMPTY_FUNCTION: () => void = Object.freeze(() => { |
6e9d10db | 5 | /* Intentionally empty */ |
4f3c3d89 | 6 | }) |
78099a15 JB |
7 | |
8 | /** | |
9 | * Returns the median of the given data set. | |
10 | * | |
11 | * @param dataSet - Data set. | |
12 | * @returns The median of the given data set. | |
13 | */ | |
14 | export const median = (dataSet: number[]): number => { | |
15 | if (Array.isArray(dataSet) && dataSet.length === 1) { | |
16 | return dataSet[0] | |
17 | } | |
18 | const sortedDataSet = dataSet.slice().sort((a, b) => a - b) | |
19 | const middleIndex = Math.floor(sortedDataSet.length / 2) | |
20 | if (sortedDataSet.length % 2 === 0) { | |
21 | return sortedDataSet[middleIndex / 2] | |
22 | } | |
23 | return (sortedDataSet[middleIndex - 1] + sortedDataSet[middleIndex]) / 2 | |
24 | } |