UNPKG

es-next-tools

Version:

A comprehensive utility library for JavaScript and TypeScript that provides a wide range of functions for common programming tasks, including mathematical operations, date manipulations, array and object handling, string utilities, and more.

19 lines (18 loc) 526 B
/** * Calculates the median of a list of numbers. * @param {...number[]} numbers - The numbers to calculate the median of. * @returns The median of the numbers. * @example * median(1, 2, 3, 4, 5); // 3 */ export function median(...numbers) { let sorted = []; if (numbers.length <= 2) sorted = numbers; else sorted = numbers.sort((a, b) => a - b); const mid = ~~(sorted.length / 2); return sorted.length % 2 !== 0 ? sorted[mid] : (sorted[mid - 1] + sorted[mid]) / 2; }