Average Numbers in a form (2)

[Total: 0    Average: 0/5]

This shows how to average numbers in a form.

Body Code:

<script>
<!-- //
var NUMBER=4; // set to be the number of numbers to average.
var _a  = '';
    _a += '<center><h2>Please Enter Any '+NUMBER+' Numbers</h2></center>';
    _a += '<center><form name=arm>';
	for (var I=0; I<NUMBER; I++)
	   _a += '<input type=text name=p'+I+' value="0" size="3">';
    _a += '<BR>Average: <input type=text size="5" name=ans value="?"><BR>';
    _a += '<input type=button value="Average" onclick="average()">';
    _a += '</form></center>';
document.write(_a);
function average() {
  var _result = 0.0;
  for (var I=0; I<NUMBER; I++) {
     var _x = eval("document.arm.p"+I+".value");
	 var _y = parseFloat(_x);
	 var _N = typeof(_y) == "number";
    _result += (_N?_y:0.0) / NUMBER;
  }
  document.arm.ans.value=_result;
}
// -->
</Script> 

Times Viewed: 0

Leave a Reply

Your email address will not be published. Required fields are marked *