Skip to content

Commit

Permalink
feat: xBoxPlot returns an object with better named properties
Browse files Browse the repository at this point in the history
This is a breaking change and the names are now
-  q1
- median
- q3
- min
- max
  • Loading branch information
lpatiny committed May 6, 2022
1 parent 2be9130 commit 125a4cc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
24 changes: 12 additions & 12 deletions src/x/__tests__/xBoxPlot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ describe('xBoxPlot', () => {
it('test xBoxPlot even', () => {
let array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
expect(xBoxPlot(array)).toStrictEqual({
Q1: 2.5,
Q2: 5.5,
Q3: 8.5,
q1: 2.5,
median: 5.5,
q3: 8.5,
min: 0,
max: 11,
});
Expand All @@ -15,9 +15,9 @@ describe('xBoxPlot', () => {
it('test xBoxPlot even small', () => {
let array = [0, 1, 2, 3, 4, 5];
expect(xBoxPlot(array)).toStrictEqual({
Q1: 1,
Q2: 2.5,
Q3: 4,
q1: 1,
median: 2.5,
q3: 4,
min: 0,
max: 5,
});
Expand All @@ -26,9 +26,9 @@ describe('xBoxPlot', () => {
it('test xBoxPlot odd', () => {
let array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
expect(xBoxPlot(array)).toStrictEqual({
Q1: 2,
Q2: 5,
Q3: 8,
q1: 2,
median: 5,
q3: 8,
min: 0,
max: 10,
});
Expand All @@ -37,9 +37,9 @@ describe('xBoxPlot', () => {
it('test xBoxPlot odd small', () => {
let array = [0, 1, 2, 3, 4];
expect(xBoxPlot(array)).toStrictEqual({
Q1: 0.5,
Q2: 2,
Q3: 3.5,
q1: 0.5,
median: 2,
q3: 3.5,
min: 0,
max: 4,
});
Expand Down
18 changes: 9 additions & 9 deletions src/x/xBoxPlot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,32 @@ export function xBoxPlot(array: DoubleArray) {
);
}
let info = {
Q1: 0.0,
Q2: 0.0,
Q3: 0.0,
q1: 0.0,
median: 0.0,
q3: 0.0,
min: array[0],
max: array[array.length - 1],
};
let q1max, q3min;
if (array.length % 2 === 1) {
// odd
let middle = (array.length - 1) / 2;
info.Q2 = array[middle];
info.median = array[middle];
q1max = middle - 1;
q3min = middle + 1;
} else {
// even
q3min = array.length / 2;
q1max = q3min - 1;
info.Q2 = (array[q1max] + array[q3min]) / 2;
info.median = (array[q1max] + array[q3min]) / 2;
}
if (q1max % 2 === 0) {
info.Q1 = array[q1max / 2];
info.Q3 = array[(array.length + q3min - 1) / 2];
info.q1 = array[q1max / 2];
info.q3 = array[(array.length + q3min - 1) / 2];
} else {
info.Q1 = (array[(q1max + 1) / 2] + array[(q1max - 1) / 2]) / 2;
info.q1 = (array[(q1max + 1) / 2] + array[(q1max - 1) / 2]) / 2;
let middleOver = (array.length + q3min) / 2;
info.Q3 = (array[middleOver] + array[middleOver - 1]) / 2;
info.q3 = (array[middleOver] + array[middleOver - 1]) / 2;
}
return info;
}

0 comments on commit 125a4cc

Please sign in to comment.