Update percentile.md

This commit is contained in:
atomiks
2017-12-13 22:18:49 +11:00
committed by GitHub
parent cd76829262
commit 19cff66dd4

View File

@ -1,18 +1,10 @@
### Percentile ### Percentile
Calculate how many numbers are below the value and how many are the same value and Use `Array.filter()` to calculate how many numbers are below the value and how many are the same value and
apply the percentile formula. apply the percentile formula.
```js ```js
const percentile = (arr, val) => { const percentile = (arr, val) =>
let below = 0, same = 0; 100 * (arr.filter(v => v < val).length + 0.5 * arr.filter(v => v === val).length) / arr.length;
for (const number of arr) {
if (number < val) below++;
if (number === val) same++;
}
return 100 * (below + (0.5 * same)) / arr.length;
};
// percentile([1,2,3,4,5,6,7,8,9,10], 6) -> 55 // percentile([1,2,3,4,5,6,7,8,9,10], 6) -> 55
``` ```