Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating median - javascript

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 why. Here's the code;

class StatsCollector {      constructor() {         this.inputNumber = 0;         this.average = 0;          this.timeout = 19000;          this.frequencies = new Map();         for (let i of Array(this.timeout).keys()) {             this.frequencies.set(i, 0);         }     }      pushValue(responseTimeMs) {         let req = responseTimeMs;         if (req > this.timeout) {             req = this.timeout;         }          this.average = (this.average * this.inputNumber + req) / (this.inputNumber + 1);          console.log(responseTimeMs / 1000)         let groupIndex = Math.floor(responseTimeMs / 1000);         this.frequencies.set(groupIndex, this.frequencies.get(groupIndex) + 1);          this.inputNumber += 1;     }      getMedian() {         let medianElement = 0;         if (this.inputNumber <= 0) {             return 0;         }         if (this.inputNumber == 1) {             return this.average         }         if (this.inputNumber == 2) {             return this.average         }         if (this.inputNumber > 2) {             medianElement = this.inputNumber / 2;         }          let minCumulativeFreq = 0;         let maxCumulativeFreq = 0;         let cumulativeFreq = 0;         let freqGroup = 0;         for (let i of Array(20).keys()) {             if (medianElement <= cumulativeFreq + this.frequencies.get(i)) {                 minCumulativeFreq = cumulativeFreq;                 maxCumulativeFreq = cumulativeFreq + this.frequencies.get(i);                 freqGroup = i;                 break;             }             cumulativeFreq += this.frequencies.get(i);         }          return (((medianElement - minCumulativeFreq) / (maxCumulativeFreq - minCumulativeFreq)) + (freqGroup)) * 1000;     }      getAverage() {         return this.average;     }  } 

Here's the snapshot of the results when I enter the values of

342,654,987,1093,2234,6243,7087,20123

enter image description here

The correct result should be;

Median: 1663.5

like image 478
Muhammed Bayram Avatar asked Jul 25 '17 16:07

Muhammed Bayram


People also ask

What is median of an array Javascript?

If the array length is even then median will be arr[(arr. length)/2] +arr[((arr. length)/2)+1]. If the array length is odd then the median will be a middle element. Javascript.

How do you calculate average in Javascript?

You calculate an average by adding all the elements and then dividing by the number of elements. var total = 0; for(var i = 0; i < grades. length; i++) { total += grades[i]; } var avg = total / grades.


2 Answers

Change your median method to this:

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 (values[half - 1] + values[half]) / 2.0; } 

fiddle

like image 70
jdmdevdotnet Avatar answered Oct 04 '22 14:10

jdmdevdotnet


Here's another solution:

function median(numbers) {      const sorted = numbers.slice().sort((a, b) => a - b);      const middle = Math.floor(sorted.length / 2);        if (sorted.length % 2 === 0) {          return (sorted[middle - 1] + sorted[middle]) / 2;      }        return sorted[middle];  }    console.log(median([4, 5, 7, 1, 33]));
like image 27
JBallin Avatar answered Oct 04 '22 14:10

JBallin