title: Calculating a student grade in JavaScript
body: <p>I've got an HTMl form to input student names and grades in which should use JavaScript to calculate a grade that is then printed on the screen. My HTMl works fine if I comment the meat of my JavaScript out, but not if I don't. The error is somewhere in the code that I commented out, but I just can't find it (or them).</p>
  
  <p>The HTML form will submit, alert the user, and show my message correctly as long as it is fed a concrete grade and score instead of trying to fetch it through getGrade() and getScore(). This is why I know the bug is in the JavaScript.</p>
  
  <p>I've run the page in the JavaScript console to look for bugs, but there are no red flag errors.  </p>
  
  <p>This is my first HTML/JavaScript program, and I not very good at it. Anyway, it's a day late and in less than two hours, it will be two days late, so if anyone could help, I'd appreciate it hugely. Here's the code, sorry if it's too much:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  
  &lt;head&gt;
    &lt;title&gt;Joe Peterson's Assignment 7&lt;/title&gt;
    &lt;script&gt;
      function calculateGrade(){
  
  
        document.getElementById("form1").style.display = "none";
        document.getElementById("p1").style.display = "block";
        var fname = document.getElementById("LastName").value;
        var lname = document.getElementById("FirstName").value;
        var valMidterm = document.getElementById("MidtermScore").value;
        var valFinal = document.getElementById("FinalScore").value;
        var lMidterm = document.getElementById("MidtermLetter").value;
        var lFinal = document.getElementById("FinalLetter").value;
        var homeworkArray = document.getElementById("HomeworkScores").value.split(',');
        var classworkArray = document.getElementById("ActivityScores").value.split(',');
   /*     
        var s1 = new Student(fname, lname);
        for (var i = 0; i &lt; classworkArray.length; i++)
          s1.addGradedActivity(classworkArray[i]);
        for (var i = 0; i &lt; homeworkArray.length; i++)
          s1.addGradedHomework(homeworkArray[i]);
        s1.setMidterm(valMidterm, lMidterm);
        s1.setFinal(valFinal, lFinal);
        var grade = s1.getGrade();      
        var score = s1.getScore();
  */      
        document.getElementById("span1").innerHTML = (grade);
        document.getElementById("span2").innerHTML = (score);
        document.getElementById("fname").innerHTML = (fname);
        document.getElementById("lname").innerHTML = (lname);
        alert(document.getElementById("FirstName").value);
        return false;
      }
   /*   
      function Coursework(){
        var sum = 0;
        var num = 0;
        this.addScore = function(score){
        if (score &lt; 0) {score = 0;}
          if (score &gt; 100) {score = 100;}
          sum += score;
          num++;
        }
        this.getAverage = function(){
          return (sum/num);
        } 
      }
  
      function Exam(){
        var score = 0;
        var grade = "";
        this.Exam = function(Score, Grade)
        {
          if (Score &lt; 0) {Score = 0;}
          if (Score &gt; 100) {Score = 100;}
          score = Score;
          if (Grade != "A" &amp;&amp; Grade != "B" &amp;&amp; Grade !="C" 
                           &amp;&amp; Grade !="D" &amp;&amp; Grade != "F")
          {
              console.log("You have submitted a bad grade for an Exam, value of:G.");
              Grade = "F";
          }
          grade = Grade;
        }
        this.getScore = function(){
          return score;
        }
        this.getGrade = function(){
          return grade;
        }
      }
  
      function Student(){
        var firstName;
        var lastName;
        var midterm;
        var finalExam;
        var homework = new Coursework();
        var inclass = new Coursework();
        this.Student = function(first, last){
          firstName = first;
          lastName = last;
        }
        this.setMidterm = function(score, grade){
          midterm = new Exam(score, grade);
        }
        this.setFinal = function(score, grade){
          finalExam = new Exam(score, grade);
        }
        this.addGradedHomework = function(score){
          homework.addScore(score);
        }
        this.addGradedActivity = function(score){
          inclass.addScore(score);
        }
  
        this.getFinalGrade = function(totalScore)
        {
          var grade = totalGrade(totalScore);
          var grade2 = "F";
          var midGrade = this.convertGrade(midterm.getGrade());
          var finGrade = this.convertGrade(finalExam.getGrade());
          if (midGrade &lt; finGrade)
          {
              grade2 = midterm.getGrade();
          }
          else
          {
              grade2 = finalExam.getGrade();
          }
          if (convertGrade(grade) &gt; convertGrade(grade2))
              return(grade);
          else
              return(grade2);
        }
  
        this.totalGrade = function(totalScore)
        {
          var totalGrade = "F";
          if (totalScore &gt;= 90)
              totalGrade = "A";
          else if (totalScore &gt;= 80)
              totalGrade = "B";
          else if (totalScore &gt;= 70)
              totalGrade = "C";
          else if (totalScore &gt;= 60)
              totalGrade = "D";
          else 
              totalGrade = "F";
          return (totalGrade);
        }
  
        this.convertGrade = function(letter)
        {
          var num = 0;
          if (letter === "A")
              num = 4;
          else if (letter === "B")
              num = 3;
          else if (letter === "C")
              num = 2;
          else if (letter === "D")
              num = 1;
          else
              num = 0;
          return(num);
        }
  
        this.getScore = function()
        {
          var totalScore = 0;
          totalScore += inclass.getAverage() * .15;
          totalScore += homework.getAverage() * .25;
          totalScore += midterm.getScore() * .25;
          totalScore += finalExam.getScore() * .35;
          return totalScore;
        }
  
        this.getGrade = function()
        {
          var totalScore = 0;
          totalScore += inclass.getAverage() * .15;
          totalScore += homework.getAverage() * .25;
          totalScore += midterm.getScore() * .25;
          totalScore += finalExam.getScore() * .35;
          var grade = getFinalGrade(totalScore);
          return grade;
        }
  
      }
  */    
  
    &lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  &lt;p&gt;&lt;h1&gt;Joe Peterson's Assignment 7&lt;/h1&gt;&lt;/p&gt;
  &lt;form name="input" id="form1" onsubmit="return calculateGrade()" style="display:form"&gt;
  First name: &lt;input type="text" name="FirstName" id="FirstName" value="Joe"&gt;&lt;br&gt;
  Last name: &lt;input type="text" name="LastName" id="LastName" value="Peterson"&gt;&lt;br&gt;
  Homework Scores (separate with commas): &lt;input type="text" name="HomeworkScores" id="HomeworkScores" value="90, 80, 98"&gt;&lt;br&gt;
  Activity Scores (separate with commas): &lt;input type="text" name="ActivityScores" id="ActivityScores" value="71, 65, 96"&gt;&lt;br&gt;
  Midterm Exam Percentage: &lt;input type="text" name="MidtermScore" id="MidtermScore" value="91"&gt;&lt;br&gt;
  Midterm Exam Letter Grade: &lt;input type="text" name="MidtermLetter" id="MidtermLetter" value="A"&gt;&lt;br&gt;
  Final Exam Percentage: &lt;input type="text" name="FinalScore" id="FinalScore" value="75"&gt;&lt;br&gt;
  Final Exam Letter Grade: &lt;input type="text" name="FinalLetter" id="FinalLetter" value="C"&gt;&lt;br&gt;
  &lt;input type="submit" value="Submit"&gt;
  &lt;/form&gt; 
  
  &lt;p id="p1" style="display:none"&gt;Final course grade for &lt;span id="fname"&gt;Joe&lt;/span&gt; &lt;span id="lname"&gt;Peterson&lt;/span&gt; is: &lt;span id="span1"&gt;X&lt;/span&gt; (&lt;span id="span2"&gt;yy&lt;/span&gt;%)&lt;/p&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>