用JavaScript计算学生成绩

时间:2014-11-05 14:37:52

标签: javascript

我有一个HTMl表单输入学生姓名和成绩,其中应该使用JavaScript来计算然后在屏幕上打印的成绩。如果我评论我的JavaScript的内容,我的HTMl工作正常,但如果我不这样做则不行。错误是我注释掉的代码中的某个地方,但我无法找到它(或者它们)。

HTML表单将提交,提醒用户,并正确显示我的消息,只要它输入具体的分数和分数,而不是尝试通过getGrade()和getScore()获取它。这就是为什么我知道这个bug存在于JavaScript中。

我在JavaScript控制台中运行该页面以查找错误,但没有红旗错误。

这是我的第一个HTML / JavaScript程序,我不是很擅长。无论如何,它是一天晚了,不到两个小时,它将延迟两天,所以如果有人能提供帮助,我会非常感激。这是代码,对不起,如果它太多了:

<!DOCTYPE html>
<html>

<head>
  <title>Joe Peterson's Assignment 7</title>
  <script>
    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 < classworkArray.length; i++)
        s1.addGradedActivity(classworkArray[i]);
      for (var i = 0; i < 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 < 0) {score = 0;}
        if (score > 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 < 0) {Score = 0;}
        if (Score > 100) {Score = 100;}
        score = Score;
        if (Grade != "A" && Grade != "B" && Grade !="C" 
                         && Grade !="D" && 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 < finGrade)
        {
            grade2 = midterm.getGrade();
        }
        else
        {
            grade2 = finalExam.getGrade();
        }
        if (convertGrade(grade) > convertGrade(grade2))
            return(grade);
        else
            return(grade2);
      }

      this.totalGrade = function(totalScore)
      {
        var totalGrade = "F";
        if (totalScore >= 90)
            totalGrade = "A";
        else if (totalScore >= 80)
            totalGrade = "B";
        else if (totalScore >= 70)
            totalGrade = "C";
        else if (totalScore >= 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;
      }

    }
*/    

  </script>
</head>

<body>
<p><h1>Joe Peterson's Assignment 7</h1></p>
<form name="input" id="form1" onsubmit="return calculateGrade()" style="display:form">
First name: <input type="text" name="FirstName" id="FirstName" value="Joe"><br>
Last name: <input type="text" name="LastName" id="LastName" value="Peterson"><br>
Homework Scores (separate with commas): <input type="text" name="HomeworkScores" id="HomeworkScores" value="90, 80, 98"><br>
Activity Scores (separate with commas): <input type="text" name="ActivityScores" id="ActivityScores" value="71, 65, 96"><br>
Midterm Exam Percentage: <input type="text" name="MidtermScore" id="MidtermScore" value="91"><br>
Midterm Exam Letter Grade: <input type="text" name="MidtermLetter" id="MidtermLetter" value="A"><br>
Final Exam Percentage: <input type="text" name="FinalScore" id="FinalScore" value="75"><br>
Final Exam Letter Grade: <input type="text" name="FinalLetter" id="FinalLetter" value="C"><br>
<input type="submit" value="Submit">
</form> 

<p id="p1" style="display:none">Final course grade for <span id="fname">Joe</span> <span id="lname">Peterson</span> is: <span id="span1">X</span> (<span id="span2">yy</span>%)</p>

</body>
</html>

0 个答案:

没有答案