I'm doing all the basic average functions (Maths)
Is there a simpler way to do this???Code:function median(elem,returnType) { if(typeof returnType == "undefined") { var returnType = "1"; } elem.sort(sortMethod); var numType; var half1 = elem.length / 2; var half2; var halfminused; var halfdivided; var plus1; var median; if(elem.length % 2) { plus1 = Math.floor(half1); //As i[1] is actually the second value in an array not the first, we've got to floor() it instead of ceil(); median = elem[plus1]; } else { half2 = half1 + 1; halfminused = half2 - half1; halfdivided = halfminused / 2; median = half1 + halfdivided; } if(returnType == "1") { return {median : median}; } else { if(returnType == "2") { return median; } } return median; }



Reply With Quote
Bookmarks