adding median

This commit is contained in:
frankknoll
2022-03-17 16:52:19 +01:00
parent 23749a21ed
commit 1e8d97e6d3
5 changed files with 37 additions and 4 deletions

9
docs/Utils.js Normal file
View File

@@ -0,0 +1,9 @@
class Utils {
// adapted from https://www.w3resource.com/javascript-exercises/fundamental/javascript-fundamental-exercise-88.php
static median(arr) {
const mid = Math.floor(arr.length / 2);
const nums = [...arr].sort((a, b) => a - b);
return arr.length % 2 !== 0 ? nums[mid] : (nums[mid - 1] + nums[mid]) / 2;
}
}