3 Replies - 1438 Views - Last Post: 27 September 2012 - 08:04 AM

#1 camhughes70  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-September 12

JS Grade Calculator- Fails to Average Values

Posted 27 September 2012 - 06:52 AM

Everything works except for when I click the average button. I know I am missing something small please help.
<html>
 <head>
    <title> Grade Calculator </title>
	<script type="text/javascript">
	  function Calculate Grade()
      // Assumes: I know what I am doing
	  // Results: gets average of grades and displays it in outputDiv
	  {
		 homework=parseFloat(document.getElementById('homework').value);
		 labs=parseFloat(document.getElementById('labs').value);
		 midterm=parseFloat(document.getElementById('midterm').value);
		 final_exam=parseFloat(document.getElementById('final_exam').value);
		 average = homework*0.25+ labs*0.20 + midterm*0.30 + final_exam*0.25;
		 document.getElementById('outputDiv').innerHTML=
		   'Your Grade Average is ' + average;
	  }
	</script> 
  </head> 
 
 <body>
 <body bgcolor="red"
 <h2> Grade Calculator </h2>
   <p>
   <table><tr><td>
    Homework </td><td><input type="text" id="homework" size=4 value=""></td></tr>
	 <br>
	 <tr><td>
	Labs </td><td>    <input type="text" id="labs" size=4 value=""></td></tr>
	 <br>
	 <tr><td>
	Midterm </td><td><input type="text" id="midterm" size=4 value=""></td></tr>
	 <br>
	 <tr><td>
	Final Exam </td><td><input type="text" id="final_exam" size=4 value=""></td></tr>
	</table>
	 <br>
	 </p>
     <p>
	 <input type="button" value="Calculate Grade"
       onclick="Calculate Grade()">
	
    <br>	
	<hr>
	<div id="outputDiv"></div>
  </body>
  </html>


This post has been edited by macosxnerd101: 27 September 2012 - 07:10 AM
Reason for edit:: Please use code tags and a descriptive title


Is This A Good Question/Topic? 0
  • +

Replies To: JS Grade Calculator- Fails to Average Values

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10563
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: JS Grade Calculator- Fails to Average Values

Posted 27 September 2012 - 07:11 AM

Moved to Javascript. Please note that Java != Javascript.
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5829
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

Re: JS Grade Calculator- Fails to Average Values

Posted 27 September 2012 - 07:33 AM

You have two body tags...

Your big issue is the function name. You simply cannot have spaces in the name. Fix that and you'll be on your way.

Your markup looks archaic. What's with all the p and br thrown about, many in invalid places? I cleaned it up a bit.
<html>
<head>
	<title> Grade Calculator </title>
	<script type="text/javascript">
		function Calculate_Grade() {
			var getFloat = function(id) { return parseFloat(document.getElementById(id).value); };
			var homework = getFloat('homework');
			var labs = getFloat('labs');
			var midterm = getFloat('midterm');
			var final_exam = getFloat('final_exam');
			var average = homework*0.25+ labs*0.20 + midterm*0.30 + final_exam*0.25;
			document.getElementById('outputDiv').innerHTML= 'Your Grade Average is ' + average;
	  }
	</script> 
</head> 
<body>
	<h2>Grade Calculator</h2>
	<table>
		<tr><td>Homework</td><td><input type="text" id="homework" size="4" value=""/ ></td></tr>
		<tr><td>Labs</td><td><input type="text" id="labs" size="4" value="" /></td></tr>
		<tr><td>Midterm</td><td><input type="text" id="midterm" size="4" value="" /></td></tr>
		<tr><td>Final Exam</td><td><input type="text" id="final_exam" size="4" value="" /></td></tr>
	</table>
	<p><input type="button" value="Calculate Grade"  onclick="Calculate_Grade()" /></p>
	<hr />
	<div id="outputDiv"></div>
</body>
</html>



Your next task would be to check if any values are NaN and scream at the user.
Was This Post Helpful? 1
  • +
  • -

#4 camhughes70  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-September 12

Re: JS Grade Calculator- Fails to Average Values

Posted 27 September 2012 - 08:04 AM

Thanks a ton. I am just starting out so alot of it is from an old book.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1