Calculating median - javascript

后端 未结 9 863
轮回少年
轮回少年 2021-02-12 09:39

I\'ve been trying to calculate median but still I\'ve got some mathematical issues I guess as I couldn\'t get the correct median value and couldn\'t figure out

9条回答
  •  闹比i
    闹比i (楼主)
    2021-02-12 10:06

    `

    var arr = {  
      max: function(array) {
        return Math.max.apply(null, array);
      },
    
      min: function(array) {
        return Math.min.apply(null, array);
      },
    
      range: function(array) {
        return arr.max(array) - arr.min(array);
      },
    
      midrange: function(array) {
        return arr.range(array) / 2;
      },
    
      sum: function(array) {
        var num = 0;
        for (var i = 0, l = array.length; i < l; i++) num += array[i];
        return num;
      },
    
      mean: function(array) {
        return arr.sum(array) / array.length;
      },
    
      median: function(array) {
        array.sort(function(a, b) {
          return a - b;
        });
        var mid = array.length / 2;
        return mid % 1 ? array[mid - 0.5] : (array[mid - 1] + array[mid]) / 2;
      },
    
      modes: function(array) {
        if (!array.length) return [];
        var modeMap = {},
          maxCount = 1,
          modes = [array[0]];
    
        array.forEach(function(val) {
          if (!modeMap[val]) modeMap[val] = 1;
          else modeMap[val]++;
    
          if (modeMap[val] > maxCount) {
            modes = [val];
            maxCount = modeMap[val];
          }
          else if (modeMap[val] === maxCount) {
            modes.push(val);
            maxCount = modeMap[val];
          }
        });
        return modes;
      },
    
      variance: function(array) {
        var mean = arr.mean(array);
        return arr.mean(array.map(function(num) {
          return Math.pow(num - mean, 2);
        }));
      },
    
      standardDeviation: function(array) {
        return Math.sqrt(arr.variance(array));
      },
    
      meanAbsoluteDeviation: function(array) {
        var mean = arr.mean(array);
        return arr.mean(array.map(function(num) {
          return Math.abs(num - mean);
        }));
      },
    
      zScores: function(array) {
        var mean = arr.mean(array);
        var standardDeviation = arr.standardDeviation(array);
        return array.map(function(num) {
          return (num - mean) / standardDeviation;
        });
      }
    };
    

    `

提交回复
热议问题