Average Numbers in a form

[Total: 0    Average: 0/5]

This shows how to average numbers in a form.

Body Code:

<center><h2>Please Enter Any 4 Numbers</h2></center> 
<center>
<form name=arm>
         <input type=text name=p1 value="0" size=3>
         <input type=text name=p2 value="0" size=3>
         <input type=text name=p3 value="0" size=3>
         <input type=text name=p4 value="0" size=3><br>
Average: <input type=text size=5 name=ans value="?"><BR>
<input type=button value="Average" onclick="average()"><BR>
</form>
</center>

<Script type=text/JavaScript>
function average(){
  var _p1=typeof(parseFloat(document.arm.p1.value))=="number"?parseFloat(document.arm.p1.value):0.0;
  var _p2=typeof(parseFloat(document.arm.p2.value))=="number"?parseFloat(document.arm.p2.value):0.0;
  var _p3=typeof(parseFloat(document.arm.p3.value))=="number"?parseFloat(document.arm.p3.value):0.0;
  var _p4=typeof(parseFloat(document.arm.p4.value))=="number"?parseFloat(document.arm.p4.value):0.0;
  var results=((_p1+_p2+_p3+_p4)/4.0) ;
  document.arm.ans.value=results;
}
</Script> 

Times Viewed: 3

Leave a Reply

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