UNPKG

es-toolkit

Version:

A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.

16 lines (14 loc) 373 B
function median(nums) { if (nums.length === 0) { return NaN; } const sorted = nums.slice().sort((a, b) => a - b); const middleIndex = Math.floor(sorted.length / 2); if (sorted.length % 2 === 0) { return (sorted[middleIndex - 1] + sorted[middleIndex]) / 2; } else { return sorted[middleIndex]; } } export { median };