Median

JavaScript, Math · Oct 22, 2020

Calculates the median of an array of numbers.

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;
};
median([5, 6, 50, 1, -5]); // 5

More like this

  • Greatest common divisor

    Calculates the greatest common divisor between two or more numbers/arrays.

    JavaScript, Math · Dec 29, 2020

  • Standard deviation

    Calculates the standard deviation of an array of numbers.

    JavaScript, Math · Oct 22, 2020

  • Percentile of matches

    Calculates the percentage of numbers in the given array that are less or equal to the given value.

    JavaScript, Math · Oct 22, 2020