From cd768292627e659fa17231a5a818ceb6cd005ef1 Mon Sep 17 00:00:00 2001 From: atomiks Date: Wed, 13 Dec 2017 21:50:16 +1100 Subject: [PATCH 1/4] Create percentile.md https://www.easycalculation.com/statistics/percentile-rank.php Feel free to try and one-linerify it if possible, lol --- snippets/percentile.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 snippets/percentile.md diff --git a/snippets/percentile.md b/snippets/percentile.md new file mode 100644 index 000000000..70d303d1a --- /dev/null +++ b/snippets/percentile.md @@ -0,0 +1,18 @@ +### Percentile + +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) => { + let below = 0, same = 0; + + 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 + ``` From 19cff66dd43adc506151cb7a2a0e1d91a47eb182 Mon Sep 17 00:00:00 2001 From: atomiks Date: Wed, 13 Dec 2017 22:18:49 +1100 Subject: [PATCH 2/4] Update percentile.md --- snippets/percentile.md | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/snippets/percentile.md b/snippets/percentile.md index 70d303d1a..4be44782a 100644 --- a/snippets/percentile.md +++ b/snippets/percentile.md @@ -1,18 +1,10 @@ ### 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. ```js -const percentile = (arr, val) => { - let below = 0, same = 0; - - for (const number of arr) { - if (number < val) below++; - if (number === val) same++; - } - - return 100 * (below + (0.5 * same)) / arr.length; -}; +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 ``` From 32d182d1f66bf73b85ad7c0c2f7b142f51699cf3 Mon Sep 17 00:00:00 2001 From: atomiks Date: Wed, 13 Dec 2017 22:23:11 +1100 Subject: [PATCH 3/4] use reduce magic --- snippets/percentile.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snippets/percentile.md b/snippets/percentile.md index 4be44782a..271a90599 100644 --- a/snippets/percentile.md +++ b/snippets/percentile.md @@ -4,7 +4,7 @@ Use `Array.filter()` to calculate how many numbers are below the value and how m 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; +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 ``` From b9edc9a85296bfdf644099bb3f4d3dafbd5fe3fc Mon Sep 17 00:00:00 2001 From: atomiks Date: Wed, 13 Dec 2017 22:23:43 +1100 Subject: [PATCH 4/4] Update percentile.md --- snippets/percentile.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/percentile.md b/snippets/percentile.md index 271a90599..eb72b711c 100644 --- a/snippets/percentile.md +++ b/snippets/percentile.md @@ -1,6 +1,6 @@ ### Percentile -Use `Array.filter()` to calculate how many numbers are below the value and how many are the same value and +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