if (Array.isArray(dataSet) && dataSet.length === 1) {
return dataSet[0]
}
- const sortedDataSet = dataSet.slice().sort((a, b) => a - b)
- const middleIndex = Math.floor(sortedDataSet.length / 2)
- if (sortedDataSet.length % 2 === 0) {
- return sortedDataSet[middleIndex]
- }
- return (sortedDataSet[middleIndex - 1] + sortedDataSet[middleIndex]) / 2
+ dataSet = [...dataSet].slice().sort((a, b) => a - b)
+ return (dataSet[(dataSet.length - 1) >> 1] + dataSet[dataSet.length >> 1]) / 2
}
export const isPlainObject = (obj: unknown): boolean =>
describe('Utils test suite', () => {
it('Verify median computation', () => {
const array = [0.25, 4.75, 3.05, 6.04, 1.01, 2.02, 5.03]
- expect(median(array)).toBe(2.535)
+ expect(median(array)).toBe(3.05)
})
it('Verify isPlainObject() behavior', () => {