Files
30-seconds-of-code/snippets/percentile.md
2017-12-13 22:18:49 +11:00

11 lines
343 B
Markdown

### Percentile
Use `Array.filter()` 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.filter(v => v < val).length + 0.5 * arr.filter(v => v === val).length) / arr.length;
// percentile([1,2,3,4,5,6,7,8,9,10], 6) -> 55
```