Nodejs Array Median median()

Here you can find the source of median()

Method Source Code

Array.prototype.median = function() {
  var length = this.length;
  if(length % 2 != 0){
    return this[Math.floor(length/2)];
  } else {/*from ww w .jav  a2  s.  c  o  m*/
    return (this[(length/2) - 1] + this[length/2])/2
  }
};

Related

  1. 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()); 
    ...
    
  2. 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];
    
  3. 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];
    };
    
  4. 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
    
  5. 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
    ...