title: Calculating median - javascript
body: <p>I've been trying to calculate <strong>median</strong> 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;</p>
  
  <pre><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 &gt; 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 &lt;= 0) {
              return 0;
          }
          if (this.inputNumber == 1) {
              return this.average
          }
          if (this.inputNumber == 2) {
              return this.average
          }
          if (this.inputNumber &gt; 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 &lt;= 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;
      }
  
  }
  </code></pre>
  
  <p>Here's the snapshot of the results when I enter the values of </p>
  
  <blockquote>
    <p>342,654,987,1093,2234,6243,7087,20123</p>
  </blockquote>
  
  <p><img src="https://i.stack.imgur.com/DkjlN.jpg" alt="enter image description here"></p>
  
  <p>The correct result should be;</p>
  
  <blockquote>
    <p>Median: 1663.5</p>
  </blockquote>