use reduce magic

This commit is contained in:
atomiks
2017-12-13 22:23:11 +11:00
committed by GitHub
parent 619c361f74
commit 60d8afaf73

View File

@ -5,6 +5,6 @@ apply the percentile formula.
```js
const percentile = (arr, val) =>
100 * (arr.filter(v => v < val).length + 0.5 * arr.filter(v => v === val).length) / arr.length;
100 * arr.reduce((acc,v) => acc + (v < val ? 1 : 0) + (v === val ? 0.5 : 0), 0) / arr.length;
// percentile([1,2,3,4,5,6,7,8,9,10], 6) -> 55
```