Nodejs Utililty Methods Array Median

List of utility methods to do Array Median

Description

The list of methods to do Array Median are organized into topic(s).

Method

median()
Array.prototype.median = function() {
  let mid = Math.floor(this.length / 2);
  if (this.length % 2 === 0){
    return (this[mid] + this[mid+1]) / 2;
  } else {
    return this[mid];
};
console.log([1,2,3,4,5,6].median()); 
...
median()
Array.prototype.median = function() {
    var middle = Math.floor(this.length / 2);
    var evenMiddle = (this[middle-1] + this[middle]) / 2;
    if (this.length % 2 == 0) {
        return evenMiddle;
    } else {
        return this[middle];
median()
Array.prototype.median = function() {
  var length = this.length;
  if(length % 2 != 0){
    return this[Math.floor(length/2)];
  } else {
    return (this[(length/2) - 1] + this[length/2])/2
};
median()
Array.prototype.median = function() {
  this.sort();
  if (this.length % 2 === 0) {
    var middle = this.length / 2 - 1;
    return (this[middle] + this[middle + 1]) / 2;
  } else {
    var middle = Math.floor(this.length / 2);
    return this[middle];
};
median()
Array.prototype.median = function() {
  if((this.length - 1) % 2 == 0){
    return this[(this.length - 1) / 2];
  else {
    var eOne = this[Math.floor((this.length - 1) / 2)]
    var eTwo = this[Math.ceil((this.length - 1) / 2)]
    return (eOne + eTwo) / 2
median()
Array.prototype.median = function() {
  var d = this.length;
  if (d % 2 == 1) {
    var b = Math.floor(d / 2);
    var c = this.sortNumber()[b]
  } else {
    var b = d / 2;
    var a = this.sortNumber();
    var c = (a[b - 1] + a[b]) / 2
...