diff --git a/js/utils.js b/js/utils.js index 01152144f8caa70aead9eeac6518b8f6fb70e11b..b6ab739197223dbbb443315434e4a85eac3f781b 100644 --- a/js/utils.js +++ b/js/utils.js @@ -387,17 +387,17 @@ function deepAssign(target, source) { } /** - * Returns the median number of an array + * Returns the median value for 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); + const + mid = Math.floor(arr.length / 2), + nums = [...arr].sort((a, b) => a - b); return arr.length % 2 === 0 ? - (arr[mid] + arr[mid - 1] / 2) : - arr[mid - 1]; + (nums[mid] + nums[mid - 1]) / 2 : + nums[mid]; }