Skip to content
Snippets Groups Projects
Commit fef3a30b authored by brickode's avatar brickode
Browse files

Updated median()

parent 5f522678
1 merge request!7350Added median()
......@@ -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];
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment