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
The correct result should be;
Median: 1663.5
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.
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.
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
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]));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With