Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Without "if" solution in Clear category for [old] Median by mdafanasev
"use strict";
function median(data) {
// We should copy array to avoid modification external data.
let sorted = data.slice().sort((a,b) => a - b);
// "- 1" makes middle pointer whole,
// if length is odd and rational, if length is even.
let middle = (data.length - 1) / 2;
// So, if length is odd, floor and ceil will be equal.
// If length is even, we will get its average.
return (sorted[Math.floor(middle)] + sorted[Math.ceil(middle)])/2;
}
Aug. 21, 2017
Comments: