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
Change your median method to this:
function median(values){ if(values.length ===0) return 0; 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