My html/javascript code will not run and I am frustrated so I am not going to be able to understand why the code is not running. We were introduced to functions and here is my attempt.

When I run the code in Firefox/Mozilla I receive an error stating that
Grades is not defined. Grades is the name of the function. When I compare it to what I have been given for a reference, I cannot see what is missing or incorrect.

Thank you,
Jill

<html>
<!--grades_function.html 				            Jill Zekowski-->
<!--zekowski_grades_function.html computes grades using text boxes for input and output-->
<!--================================================================================-->

			
<head>
   <title>Jill's Homework Assignment </title>

      <script type="text"/javascript">
         function Grades()
           {               
               finalexam = parseFloat(finalexam);
 		midterm = parseFloat(midterm);
    		labwork = parseFloat(labwork);
                homework = parseFloat(homework);

             homework = document.getElementById("homeworkbox").value;    //value in homeworkbox is assigned to homework
              labwork = document.getElementById("labworkbox").value;	 //value in labworkbox is assigned to labwork
               midterm = document.getElementById("midtermbox").value;      //value in midtermbox is assigned to midterm
                finalexam = document.getElementById("finalexambox").value;  //value in finalexambox is assigned to finalexam

                 finalgrade = (.25 * homework) + (.20 * labwork) + (.25 * midterm) +(.30 * finalexam);

	     document.getElementById("finalgradebox").value = finalgrade;
            }

       </script>
</head>

  <body>
      <h2>Chapter 5 Homework</h2>
        <hr />

   <p>
      Please enter your name:
      <input type="text" id="namebox" size="15" value=" "/>
       </p>

    <p>
      Please enter your average homework grade:
      <input type="text" id="homeworkbox" size="10" value=" "/>
       </p>

   <p>
      Please enter your average labwork grade:
      <input type="text" id="labworkbox" size="10" value=" "/>
       </p>

   <p>
      Please enter your midterm grade:
      <input type="text" id="midtermbox" size="10" value=" "/>
       </p>


   <p>
      Please enter your final exam grade:
      <input type="text" id="finalexambox" size="10" value=" "/>
       </p>

   <p>
      <input type="button" value="Click to compute your final grade"  onclick="Grades();"/>
       </p>

<hr />   		 
    		 
    <p>
    The final grade for the course is:
    <input type="text" id="finalgradebox" size="10" value="" />
       </p>

	</script>

     </body>
</html>

Recommended Answers

All 2 Replies

<script type="text/javascript">
function Grades() {
homework = document.getElementById("homeworkbox").value; 
labwork = document.getElementById("labworkbox").value; 
midterm = document.getElementById("midtermbox").value; 
finalexam = document.getElementById("finalexambox").value; 
finalexam = parseFloat(finalexam);
midterm = parseFloat(midterm);
labwork = parseFloat(labwork);
homework = parseFloat(homework);
finalgrade = (.25 * homework) + (.20 * labwork) + (.25 * midterm) +(.30 * finalexam);
document.getElementById("finalgradebox").value = finalgrade; }
</script>

there is an extra quote in the script type definintion
"text"/javascript"
&
you were operating on variables finalexam|midterm|labwork|homework before they were defined
it is easy to do

<script type="text/javascript">
function Grades() {
finalexam = parseFloat(document.getElementById("finalexambox").value);
midterm = parseFloat(document.getElementById("midtermbox").value);
labwork = parseFloat(document.getElementById("labworkbox").value);
homework = parseFloat(document.getElementById("homeworkbox").value);
finalgrade = (.25 * homework) + (.20 * labwork) + (.25 * midterm) +(.30 * finalexam);
document.getElementById("finalgradebox").value = finalgrade; }
</script>

probably runs faster

You helped me! The code is good now.

Thank you,
Jill

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.