Update snippet descriptions

This commit is contained in:
Isabelle Viktoria Maciohsek
2020-10-22 20:24:04 +03:00
parent aedcded750
commit d35575373f
41 changed files with 148 additions and 88 deletions

View File

@ -3,13 +3,18 @@ title: percentile
tags: math,intermediate
---
Uses the percentile formula to calculate how many numbers in the given array are less or equal to the given value.
Calculates the percentage of numbers in the given array that are less or equal to the given value.
- Use `Array.prototype.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;
(100 *
arr.reduce(
(acc, v) => acc + (v < val ? 1 : 0) + (v === val ? 0.5 : 0),
0
)) /
arr.length;
```
```js