1 Replies - 579 Views - Last Post: 20 March 2013 - 05:34 PM

#1 ITguy94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 20-March 13

Trouble with an Average Calculator

Posted 20 March 2013 - 03:34 PM

Hello, I am have trouble with making an average counter with a list of 5 values.
I can;t get get average to update.
Also if you see Parse int, I was trying to make all values it to get correct calculations.
Thanks for your help.
<!DOCTYPE HTML>
<html>
<head>
<title></title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function calcAvg( ){
  var Avg = document.forms["Average"];
  ParseInt(var b1) = Number(df.Box1.value), 
  ParseInt(b2) = Number(df.Box2.value),
  ParseInt (b3) = Number(df.Box3.value), 
  ParseInt(b4) = Number(df.Box4.value),
  ParseInt(b5) = Number(df.Box5.value);
  var total = Number((b1+b2+b3+b4+b5)/5);
      df.Result = total;
}
</script>
</head>
<body>
<form name="Average">
Box 1<input type="text" name="Box1"  value="0"  onchange="calcAvg()" > <br>
Box 2<input type="text" name="Box2"  value="0" onchange="calcAvg()"> <br> 
Box 3<input type="text" name=" Box3"  value="0" onchange="calAvg()"> <br>
Box 4<input type="text" name="Box4"  value="0" onchange="calcAvg()"> <br>
Box 5<input type="text" name="Box5"  value="0" onchange=
"calcAvg()"> <br><br>
Total<input type="text" name="total"  value="0" onchange="ParseFloat(calcAvg())">
</form>
</body>
</html>



Is This A Good Question/Topic? 0
  • +

Replies To: Trouble with an Average Calculator

#2 JMRKER  Icon User is offline

  • D.I.C Addict

Reputation: 128
  • View blog
  • Posts: 826
  • Joined: 25-October 08

Re: Trouble with an Average Calculator

Posted 20 March 2013 - 05:34 PM

You have a number of syntax errors and typographical mistakes.
Watch the error console if using FF or Chrome browsers.
See the number of changes between your code and this working example,
including order of elements and script tags.
<!DOCTYPE HTML>
<html>
<head>
<title></title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>

<form id="myForm" action="" method="post" onsubmit="return false">
 Box 1 <input type="text" id="Box1" value="" onkeyup="calcAvg()"> <br>
 Box 2 <input type="text" id="Box2" value="" onkeyup="calcAvg()"> <br> 
 Box 3 <input type="text" id="Box3" value="" onkeyup="calcAvg()"> <br>
 Box 4 <input type="text" id="Box4" value="" onkeyup="calcAvg()"> <br>
 Box 5 <input type="text" id="Box5" value="" onkeyup="calcAvg()"> <p>
 Total <input type="text" id="Total" value="" readonly> <br>
 Average <input type="text" id="Average" value="" readonly>
</form>

<script type="text/javascript">
function calcAvg( ){
  var b1 = Number(document.getElementById('Box1').value);
  var b2 = Number(document.getElementById('Box2').value);
  var b3 = Number(document.getElementById('Box3').value); 
  var b4 = Number(document.getElementById('Box4').value);
  var b5 = Number(document.getElementById('Box5').value);
  var Total = b1+b2+b3+b4+b5;
  document.getElementById('Total').value = Total;
  document.getElementById('Average').value = Total/5;
}
calcAvg();
</script>

</body>
</html>


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1