diff --git a/js/utils.js b/js/utils.js index 686924dddbc990327c758782b4328180e6df96da..01152144f8caa70aead9eeac6518b8f6fb70e11b 100644 --- a/js/utils.js +++ b/js/utils.js @@ -385,3 +385,19 @@ function deepAssign(target, source) { } } } + +/** + * Returns the median number of an array + * For more information about mean vs. median see + * https://www.clinfo.eu/mean-median/ + * @param {[number]} arr Does not need to be sorted + */ +function median(arr = []) { + arr.sort(); + + const mid = Math.ceil(arr.length / 2); + + return arr.length % 2 === 0 ? + (arr[mid] + arr[mid - 1] / 2) : + arr[mid - 1]; +}