calculate median javacript code example

Example 1: javascript median of array

//#Source https://bit.ly/2neWfJ2 
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;
};
console.log(median([5, 6, 50, 1, -5]));
console.log(median([1, 2, 3, 4, 5]));

Example 2: how to get the median in typescript

export function median(pool: number[]): number {
  return pool[Math.floor((pool.length - 1) / 2)];
}