Merge pull request #65 from atomiks/patch-3

Create percentile.md
This commit is contained in:
Angelos Chalaris
2017-12-13 13:28:08 +02:00
committed by GitHub

10
snippets/percentile.md Normal file
View File

@ -0,0 +1,10 @@
### Percentile
Use `Array.reduce()` to calculate how many numbers are below the value and how many are the same value and
apply the percentile formula.
```js
const percentile = (arr, val) =>
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
```