//#Source https://bit.ly/2neWfJ2
const median = arr => {
const mid = Math.floor(arr.length / 2),
nums = [...arr].sort((a, b) => a - b);
return arr.length % 2 !== 0 ? nums[mid] : (nums[mid - 1] + nums[mid]) / 2;
};
console.log(median([5, 6, 50, 1, -5]));
console.log(median([1, 2, 3, 4, 5]));
function median(values){
if(values.length === 0) throw new Error("No inputs");
values.sort(function(a,b){
return a-b;
});
var half = Math.floor(values.length / 2);
if (values.length % 2)
return values[half];
return (Number(values[half - 1]) + Number(values[half])) / 2;
}
console.log(median([5, 6, 50, 1, -5])) // 5
console.log(median([1, 2, 3, 4, 5])); // 3
function median(values){
if(values.length === 0) throw new Error("No inputs");
values.sort(function(a,b){
return a-b;
});
var half = Math.floor(values.length / 2);
if (values.length % 2)
return values[half];
return (Number(values[half - 1]) + Number(values[half])) / 2;
}
console.log(median([5, 6, 50, 1, -5])) // 5
console.log(median([1, 2, 3, 4, 5])); // 3