PDA

View Full Version : Resolved Math (average)

keyboard
05-01-2012, 03:24 AM
This isn't working...

<html>
<script type="text/javascript" src="math.js"></script>
<body>
<script type="text/javascript">
var myCars=new Array();
myCars[0]="1";
myCars[1]="2";
myCars[2]="3";
myCars[3]="4";
myCars[4]="5";
myCars[5]="6";
</script>
</body>
</html>

function mean(elem) {
var meanTotal = 0;
for (i=0;i<elem.length;i++) {
var meanTotal = meanTotal + elem[i];
}
meanTotal = meanTotal / elem.length;
return meanTotal;
}
/*
var a = xyz();
document.write('x=' + a.x + ' and y = ' + a.y);
*/

Any help???

05-01-2012, 09:20 AM
Hi there keyboard1333,

try it like this...

function mean(elem) {
var meanTotal=0;
for(i=0;i<elem.length;i++) {
meanTotal+=parseFloat(elem[i])/elem.length;
}
return meanTotal;
}

vwphillips
05-01-2012, 09:24 AM
<html>
<script type="text/javascript" src="math.js"></script>
<body>
<script type="text/javascript">
function mean(elem) {
var meanTotal = 0;
for (var i=0;i<elem.length;i++) {
// the array fields are strings so ato type(convert~) to numbers
meanTotal += elem[i]*1;
}
meanTotal = meanTotal / elem.length;
return meanTotal;
}

var myCars=[];
myCars[0]="1";
myCars[1]="2";
myCars[2]="3";
myCars[3]="4";
myCars[4]="5";
myCars[5]="6";