adding median
This commit is contained in:
9
docs/Utils.js
Normal file
9
docs/Utils.js
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user