Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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]));
Comment

the median of the list javascript

function median(values){
    if(values.length === 0) throw new Error("No inputs");
    values.sort(function(a,b){
      return a-b;
    });
    var half = Math.floor(values.length / 2);
    if (values.length % 2)
      return values[half];
    return (Number(values[half - 1]) + Number(values[half])) / 2;
}

console.log(median([5, 6, 50, 1, -5])) // 5
console.log(median([1, 2, 3, 4, 5])); // 3
Comment

the median of the list javascript

function median(values){
    if(values.length === 0) throw new Error("No inputs");
    values.sort(function(a,b){
      return a-b;
    });
    var half = Math.floor(values.length / 2);
    if (values.length % 2)
      return values[half];
    return (Number(values[half - 1]) + Number(values[half])) / 2;
}

console.log(median([5, 6, 50, 1, -5])) // 5
console.log(median([1, 2, 3, 4, 5])); // 3
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript angle equation of a line 
Javascript :: eslint change max line length 
Javascript :: a tag do nothing on click 
Javascript :: update react app 
Javascript :: if str contains jquery 
Javascript :: javascript format currency 
Javascript :: detecting screen width in jquery 
Javascript :: how express serve public folder 
Javascript :: javascript remove parentheses 
Javascript :: add classnames to body 
Javascript :: javascript convert seconds to minutes seconds 
Javascript :: javascript live time 
Javascript :: how to remove the last character of a string in javascript 
Javascript :: parsefloat jquery 
Javascript :: javascript store array in localstorage 
Javascript :: turn object into string javascript 
Javascript :: angular for loop key value 
Javascript :: check if parameter is array javascript 
Javascript :: Jest encountered an unexpected token 
Javascript :: how to drawImage on center of canvas 
Javascript :: countup on scroll react only once 
Javascript :: js response to json log 
Javascript :: datatable default sorting 
Javascript :: get search params from url react 
Javascript :: puppeteer how to serch for text 
Javascript :: regex valid day 
Javascript :: jquery select by data attribute 
Javascript :: set text of dom element 
Javascript :: convert 2 dimensional array to js object 
Javascript :: readonly attribute in html by javascript 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =