}
public static median(dataSet: number[]): number {
+ if (Utils.isEmptyArray(dataSet)) {
+ return 0;
+ }
if (Array.isArray(dataSet) === true && dataSet.length === 1) {
return dataSet[0];
}
}
// TODO: use order statistics tree https://en.wikipedia.org/wiki/Order_statistic_tree
- public static percentile(dataSet: number[], percentile: number): number | undefined {
+ public static percentile(dataSet: number[], percentile: number): number {
if (percentile < 0 && percentile > 100) {
throw new RangeError('Percentile is not between 0 and 100');
}
if (Utils.isEmptyArray(dataSet)) {
- return undefined;
+ return 0;
}
const sortedDataSet = dataSet.slice().sort((a, b) => a - b);
if (percentile === 0 || sortedDataSet.length === 1) {
});
it('Verify median()', () => {
+ expect(Utils.median([])).toBe(0);
const array0 = [0.08];
expect(Utils.median(array0)).toBe(0.08);
const array1 = [0.25, 4.75, 3.05, 6.04, 1.01, 2.02, 5.03];
});
it('Verify percentile()', () => {
- expect(Utils.percentile([], 25)).toBe(undefined);
+ expect(Utils.percentile([], 25)).toBe(0);
const array0 = [0.08];
expect(Utils.percentile(array0, 50)).toBe(0.08);
const array1 = [0.25, 4.75, 3.05, 6.04, 1.01, 2.02, 5.03];