0

Get Median of array of numbers

Function JavaScript 2 revisions 58 a month ago a month ago
function median(array) {
	array = array.sort((a, b) => a - b);
	if (array.length % 2 === 0) // even
		return (array[array.length/2] + array[(array.length / 2) - 1]) / 2;
	else
		return array[(array.length - 1) / 2]; // odd
}

Usage is as expected.