Skip to content

Commit

Permalink
fix: correct median aggregation calculation
Browse files Browse the repository at this point in the history
Closes #2588
  • Loading branch information
tannerlinsley committed Aug 3, 2020
1 parent 8c1ccc9 commit 6a9223b
Showing 1 changed file with 3 additions and 11 deletions.
14 changes: 3 additions & 11 deletions src/aggregations.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,9 @@ export function median(values) {
return null
}

let min = 0
let max = 0

values.forEach(value => {
if (typeof value === 'number') {
min = Math.min(min, value)
max = Math.max(max, value)
}
})

return (min + max) / 2
const mid = Math.floor(values.length / 2)
const nums = [...values].sort((a, b) => a - b)
return values.length % 2 !== 0 ? nums[mid] : (nums[mid - 1] + nums[mid]) / 2
}

export function unique(values) {
Expand Down

1 comment on commit 6a9223b

@vercel
Copy link

@vercel vercel bot commented on 6a9223b Aug 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.