2016-03-29 16 views
-1

Kann mir jemand helfen, herauszufinden, was ich mit meinem gpa Rechner falsch mache? Ich arbeite an einer Webseite, auf der Benutzer Buchstaben für drei bestimmte Klassen eingeben können. Ich habe den HTML- und JavaScript-Code erstellt, der Buchstabengrade in Punktwerte umwandelt. Ich habe auch eine Funktion erstellt, die die Punktnoten addiert und dann durch vier teilt, um ein gpa für die drei Klassen auszugeben. Ich habe mehrere Wochen daran gearbeitet und kann nicht herausfinden, was ich falsch mache. Ich bin mir sicher, dass es viele verschiedene Möglichkeiten gibt, das zu tun, was ich versuche. Ich bin jedoch auf der Suche nach einem Weg, um mich zu codieren, weil die Art, wie ich den Code geschrieben habe, für mich am sinnvollsten ist.Javascript GPA Rechner

Dank

<!DOCTYPE html> <html> 
 

 
<head> <meta charset="utf-8"> <link rel="stylesheet" type="text/css" 
 
href="styles.css"> <title>Center for Health Information Technology 
 
</title> 
 
\t <script type="text/javascript"> 
 
\t function calculate() 
 
\t { 
 
\t \t var x=document.getElementById("result1").value; 
 
\t \t var y=document.getElementById("result2").value; 
 
\t \t var z=document.getElementById("result3").value; 
 
\t \t var a=document.getElementById("result4").value; 
 
\t \t var result1=parseFloat(x) + parseFloat(y) + parseFloat(z) 
 
\t \t + parseFloat(a); // conversion to numbers; this is necessary for +, other operators will automatically convert 
 
\t \t var result5=x%y; 
 
\t \t 
 
\t \t //build a string first and then display it all together. 
 
\t \t //don't forget HTML tags in your output! 
 

 
\t \t var html="<p>Your GPA is: "+result1/4+"</p>"; 
 
\t 
 

 

 
\t \t 
 
\t \t document.getElementById("total").innerHTML=html; 
 
\t } 
 
\t 
 
\t 
 
\t </script> 
 
</head> 
 

 
<body> 
 

 
<h1> <a href="http://kennesaw.edu "> <img src="images/ksu.jpg" alt="KSU 
 
    Graphic"/> </a>CCSE Graduate Certificate in Health Information 
 
    Technology<a href="http://kennesaw.edu "> <img src="images/ksu.jpg" 
 
    alt="KSU Graphic"/> </a> 
 
</h1> 
 

 
<nav> <a href="index.html">Home</a> | <a 
 
    href="faculty.html"> Faculty</a> | <a href="third.html"> 
 
    Miscellaneous</a> 
 
</nav> 
 

 
<p>The summer is coming and Cert HIT has some student assistant positions open. 
 
In order to be considered for such a position, a student has to complete the following 
 
4 classes and have an average grade of 3.5 or above: 
 
IT 3503 Foundation of HIT, IT 4153 Advanced Database, IT 5443 Web Development, & IT 5413 
 
Software Design and Development. Please enter the grades you have received in the four 
 
classes. 
 
</p> 
 

 
<script language="JavaScript"> 
 

 
function calculateGpa (form) { 
 
    var gpa = form.gpa1.value; 
 

 
     if (gpa == "A") { 
 
      gpa = 4; 
 
      } 
 
     if (gpa == "B") { 
 
      gpa = 3; 
 
      } 
 
     if (gpa == "C") { 
 
      gpa = 2; 
 
      } 
 
     if (gpa == "D") { 
 
      gpa = 1; 
 
     \t } 
 
     if (gpa == "F") { 
 
      gpa = 0; 
 
     \t } 
 
     if (gpa == "a") { 
 
      gpa = 4; 
 
      } 
 
     if (gpa == "b") { 
 
      gpa = 3; 
 
      } 
 
     if (gpa == "c") { 
 
      gpa = 2; 
 
      } 
 
     if (gpa == "d") { 
 
      gpa = 1; 
 
     \t } 
 
     if (gpa == "f") { 
 
      gpa = 0; 
 
     \t } 
 
    document.getElementById("result1").innerHTML = gpa; 
 
} 
 
</script> 
 

 

 
<body> 
 

 
<form action="" id="it3503">Enter your grade in the form of a letter grade for class IT 3503:<br> 
 

 
<input type="text" name="gpa1" value=""> 
 
<input type="button" name="button" value="Calculate" onClick="calculateGpa(this.form)"> 
 
</form> 
 
<div id="result1"></div> 
 

 

 
<script language="JavaScript"> 
 

 
function calculateGpa2 (form) { 
 
    var gpa = form.gpa2.value; 
 

 
     if (gpa == "A") { 
 
      gpa = 4; 
 
      } 
 
     if (gpa == "B") { 
 
      gpa = 3; 
 
      } 
 
     if (gpa == "C") { 
 
      gpa = 2; 
 
      } 
 
     if (gpa == "D") { 
 
      gpa = 1; 
 
     \t } 
 
     if (gpa == "F") { 
 
      gpa = 0; 
 
     \t } 
 
     if (gpa == "a") { 
 
      gpa = 4; 
 
      } 
 
     if (gpa == "b") { 
 
      gpa = 3; 
 
      } 
 
     if (gpa == "c") { 
 
      gpa = 2; 
 
      } 
 
     if (gpa == "d") { 
 
      gpa = 1; 
 
     \t } 
 
     if (gpa == "f") { 
 
      gpa = 0; 
 
     \t } 
 
    document.getElementById("result2").innerHTML = gpa; 
 
} 
 
</script> 
 

 

 
<body> 
 

 
<form action="" id="it4153">Enter your grade in the form of a letter grade for class IT 4153:<br> 
 

 
<input type="text" name="gpa2" value=""> 
 
<input type="button" name="button" value="Calculate" onClick="calculateGpa2(this.form)"> 
 
</form> 
 
<div id="result2"></div> 
 

 

 
<script language="JavaScript"> 
 
function calculateGpa3 (form) { 
 
    var gpa = form.gpa3.value; 
 

 
     if (gpa == "A") { 
 
      gpa = 4; 
 
      } 
 
     if (gpa == "B") { 
 
      gpa = 3; 
 
      } 
 
     if (gpa == "C") { 
 
      gpa = 2; 
 
      } 
 
     if (gpa == "D") { 
 
      gpa = 1; 
 
     \t } 
 
     if (gpa == "F") { 
 
      gpa = 0; 
 
     \t } 
 
     if (gpa == "a") { 
 
      gpa = 4; 
 
      } 
 
     if (gpa == "b") { 
 
      gpa = 3; 
 
      } 
 
     if (gpa == "c") { 
 
      gpa = 2; 
 
      } 
 
     if (gpa == "d") { 
 
      gpa = 1; 
 
     \t } 
 
     if (gpa == "f") { 
 
      gpa = 0; 
 
     \t } 
 
    document.getElementById("result3").innerHTML = gpa; 
 
} 
 
</script> 
 

 

 
<body> 
 

 
<form action="" id="it5443">Enter your grade in the form of a letter grade for class IT 5443:<br> 
 

 
<input type="text" name="gpa3" value=""> 
 
<input type="button" name="button" value="Calculate" onClick="calculateGpa3(this.form)"> 
 
</form> 
 
<div id="result3"></div> 
 

 

 
<script language="JavaScript"> 
 
function calculateGpa4(form) { 
 
    var gpa = form.gpa4.value; 
 

 
     if (gpa == "A") { 
 
      gpa = 4; 
 
      } 
 
     if (gpa == "B") { 
 
      gpa = 3; 
 
      } 
 
     if (gpa == "C") { 
 
      gpa = 2; 
 
      } 
 
     if (gpa == "D") { 
 
      gpa = 1; 
 
     \t } 
 
     if (gpa == "F") { 
 
      gpa = 0; 
 
     \t } 
 
     if (gpa == "a") { 
 
      gpa = 4; 
 
      } 
 
     if (gpa == "b") { 
 
      gpa = 3; 
 
      } 
 
     if (gpa == "c") { 
 
      gpa = 2; 
 
      } 
 
     if (gpa == "d") { 
 
      gpa = 1; 
 
     \t } 
 
     if (gpa == "f") { 
 
      gpa = 0; 
 
     \t } 
 
    document.getElementById("result4").innerHTML = gpa; 
 

 

 
} 
 
</script> 
 

 

 
<body> 
 

 
<form action="" id="it5413">Enter your grade in the form of a letter grade for class IT 5413:<br> 
 

 
<input type="text" name="gpa4" value=""> 
 
<input type="button" name="button" value="Calculate" onClick="calculateGpa4(this.form)"> 
 
</form> 
 
<div id="result4"></div> 
 
<br> 
 
<input type="button" id="final" name="button" value="Calculate" onClick="calculate()"> 
 

 
<script type="text/javascript"> 
 
\t function calculate() 
 
\t { 
 
\t \t var x=document.getElementById("result1").value; 
 
\t \t var y=document.getElementById("result2").value; 
 
\t \t var z=document.getElementById("result3").value; 
 
\t \t var a=document.getElementById("result4").value; 
 
\t \t var result1=parseFloat(x) + parseFloat(y) + parseFloat(z) 
 
\t \t + parseFloat(a); // conversion to numbers; this is necessary for +, other operators will automatically convert 
 
\t \t var result5=x%y; 
 
\t \t 
 
\t \t //build a string first and then display it all together. 
 
\t \t //don't forget HTML tags in your output! 
 

 
\t \t var html="<p>Your GPA is: "+result1/4+"</p>"; 
 
\t 
 

 

 
\t \t 
 
\t \t document.getElementById("total").innerHTML=html; 
 
\t } 
 
\t 
 
\t 
 
\t </script> 
 

 

 

 

 

 
<div id="total"></div> 
 
<br><br><br><br><br> <br><br><br><br><br><br> 
 
<div id="result"></div> 
 
</body> 
 

 

 
</html>

+0

Sie den numerischen Grad in '.innerHTML' setzen, aber dann' calculate() 'versucht es mit' .value' zu ​​lesen. Es muss '.innerHTML' verwendet werden, um es zu lesen. – Barmar

Antwort

1

Nun, Ihr Hauptproblem ist, dass Sie mit der Wertfunktion die Numberic Daten aus result1-result4 zu bekommen versuchen. Ein div-Element hat jedoch keine Wertfunktion, Sie müssen innerText verwenden.

Es gibt einige andere Probleme: Sie haben calculate() zweimal definiert, und es gibt keine Möglichkeit zu wissen, welche ausgeführt wird - ich habe die eine am Ende gelöscht.

Sie haben auch 4 offene Tags, Sie sollten nur einen offenen & close-Body-Tag haben.

<!DOCTYPE html> <html> 
 

 
<head> <meta charset="utf-8"> <link rel="stylesheet" type="text/css" 
 
href="styles.css"> <title>Center for Health Information Technology 
 
</title> 
 
\t <script type="text/javascript"> 
 
\t function calculate() 
 
\t { 
 
\t \t var x=document.getElementById("result1").innerText; 
 
\t \t var y=document.getElementById("result2").innerText; 
 
\t \t var z=document.getElementById("result3").innerText; 
 
\t \t var a=document.getElementById("result4").innerText; 
 
\t \t var result1=parseFloat(x) + parseFloat(y) + parseFloat(z) 
 
\t \t + parseFloat(a); // conversion to numbers; this is necessary for +, other operators will automatically convert 
 
\t \t var result5=x%y; 
 
\t \t 
 
\t \t //build a string first and then display it all together. 
 
\t \t //don't forget HTML tags in your output! 
 

 
\t \t var html="<p>Your GPA is: "+result1/4+"</p>"; 
 
\t 
 

 

 
\t \t 
 
\t \t document.getElementById("total").innerHTML=html; 
 
\t } 
 
\t 
 
\t 
 
\t </script> 
 
</head> 
 

 
<body> 
 

 
<h1> <a href="http://kennesaw.edu "> <img src="images/ksu.jpg" alt="KSU 
 
    Graphic"/> </a>CCSE Graduate Certificate in Health Information 
 
    Technology<a href="http://kennesaw.edu "> <img src="images/ksu.jpg" 
 
    alt="KSU Graphic"/> </a> 
 
</h1> 
 

 
<nav> <a href="index.html">Home</a> | <a 
 
    href="faculty.html"> Faculty</a> | <a href="third.html"> 
 
    Miscellaneous</a> 
 
</nav> 
 

 
<p>The summer is coming and Cert HIT has some student assistant positions open. 
 
In order to be considered for such a position, a student has to complete the following 
 
4 classes and have an average grade of 3.5 or above: 
 
IT 3503 Foundation of HIT, IT 4153 Advanced Database, IT 5443 Web Development, & IT 5413 
 
Software Design and Development. Please enter the grades you have received in the four 
 
classes. 
 
</p> 
 

 
<script language="JavaScript"> 
 

 
function calculateGpa (form) { 
 
    var gpa = form.gpa1.value; 
 

 
     if (gpa == "A") { 
 
      gpa = 4; 
 
      } 
 
     if (gpa == "B") { 
 
      gpa = 3; 
 
      } 
 
     if (gpa == "C") { 
 
      gpa = 2; 
 
      } 
 
     if (gpa == "D") { 
 
      gpa = 1; 
 
     \t } 
 
     if (gpa == "F") { 
 
      gpa = 0; 
 
     \t } 
 
     if (gpa == "a") { 
 
      gpa = 4; 
 
      } 
 
     if (gpa == "b") { 
 
      gpa = 3; 
 
      } 
 
     if (gpa == "c") { 
 
      gpa = 2; 
 
      } 
 
     if (gpa == "d") { 
 
      gpa = 1; 
 
     \t } 
 
     if (gpa == "f") { 
 
      gpa = 0; 
 
     \t } 
 
    document.getElementById("result1").innerHTML = gpa; 
 
} 
 
</script> 
 

 

 
<body> 
 

 
<form action="" id="it3503">Enter your grade in the form of a letter grade for class IT 3503:<br> 
 

 
<input type="text" name="gpa1" value=""> 
 
<input type="button" name="button" value="Calculate" onClick="calculateGpa(this.form)"> 
 
</form> 
 
<div id="result1"></div> 
 

 

 
<script language="JavaScript"> 
 

 
function calculateGpa2 (form) { 
 
    var gpa = form.gpa2.value; 
 

 
     if (gpa == "A") { 
 
      gpa = 4; 
 
      } 
 
     if (gpa == "B") { 
 
      gpa = 3; 
 
      } 
 
     if (gpa == "C") { 
 
      gpa = 2; 
 
      } 
 
     if (gpa == "D") { 
 
      gpa = 1; 
 
     \t } 
 
     if (gpa == "F") { 
 
      gpa = 0; 
 
     \t } 
 
     if (gpa == "a") { 
 
      gpa = 4; 
 
      } 
 
     if (gpa == "b") { 
 
      gpa = 3; 
 
      } 
 
     if (gpa == "c") { 
 
      gpa = 2; 
 
      } 
 
     if (gpa == "d") { 
 
      gpa = 1; 
 
     \t } 
 
     if (gpa == "f") { 
 
      gpa = 0; 
 
     \t } 
 
    document.getElementById("result2").innerHTML = gpa; 
 
} 
 
</script> 
 

 

 
<body> 
 

 
<form action="" id="it4153">Enter your grade in the form of a letter grade for class IT 4153:<br> 
 

 
<input type="text" name="gpa2" value=""> 
 
<input type="button" name="button" value="Calculate" onClick="calculateGpa2(this.form)"> 
 
</form> 
 
<div id="result2"></div> 
 

 

 
<script language="JavaScript"> 
 
function calculateGpa3 (form) { 
 
    var gpa = form.gpa3.value; 
 

 
     if (gpa == "A") { 
 
      gpa = 4; 
 
      } 
 
     if (gpa == "B") { 
 
      gpa = 3; 
 
      } 
 
     if (gpa == "C") { 
 
      gpa = 2; 
 
      } 
 
     if (gpa == "D") { 
 
      gpa = 1; 
 
     \t } 
 
     if (gpa == "F") { 
 
      gpa = 0; 
 
     \t } 
 
     if (gpa == "a") { 
 
      gpa = 4; 
 
      } 
 
     if (gpa == "b") { 
 
      gpa = 3; 
 
      } 
 
     if (gpa == "c") { 
 
      gpa = 2; 
 
      } 
 
     if (gpa == "d") { 
 
      gpa = 1; 
 
     \t } 
 
     if (gpa == "f") { 
 
      gpa = 0; 
 
     \t } 
 
    document.getElementById("result3").innerHTML = gpa; 
 
} 
 
</script> 
 

 

 
<body> 
 

 
<form action="" id="it5443">Enter your grade in the form of a letter grade for class IT 5443:<br> 
 

 
<input type="text" name="gpa3" value=""> 
 
<input type="button" name="button" value="Calculate" onClick="calculateGpa3(this.form)"> 
 
</form> 
 
<div id="result3"></div> 
 

 

 
<script language="JavaScript"> 
 
function calculateGpa4(form) { 
 
    var gpa = form.gpa4.value; 
 

 
     if (gpa == "A") { 
 
      gpa = 4; 
 
      } 
 
     if (gpa == "B") { 
 
      gpa = 3; 
 
      } 
 
     if (gpa == "C") { 
 
      gpa = 2; 
 
      } 
 
     if (gpa == "D") { 
 
      gpa = 1; 
 
     \t } 
 
     if (gpa == "F") { 
 
      gpa = 0; 
 
     \t } 
 
     if (gpa == "a") { 
 
      gpa = 4; 
 
      } 
 
     if (gpa == "b") { 
 
      gpa = 3; 
 
      } 
 
     if (gpa == "c") { 
 
      gpa = 2; 
 
      } 
 
     if (gpa == "d") { 
 
      gpa = 1; 
 
     \t } 
 
     if (gpa == "f") { 
 
      gpa = 0; 
 
     \t } 
 
    document.getElementById("result4").innerHTML = gpa; 
 

 

 
} 
 
</script> 
 

 

 
<body> 
 

 
<form action="" id="it5413">Enter your grade in the form of a letter grade for class IT 5413:<br> 
 

 
<input type="text" name="gpa4" value=""> 
 
<input type="button" name="button" value="Calculate" onClick="calculateGpa4(this.form)"> 
 
</form> 
 
<div id="result4"></div> 
 
<br> 
 
<input type="button" id="final" name="button" value="Calculate" onClick="calculate()"> 
 

 

 

 

 

 

 
<div id="total"></div> 
 
<br><br><br><br><br> <br><br><br><br><br><br> 
 
<div id="result"></div> 
 
</body> 
 

 

 
</html>

+0

* innerText * ist eine proprietäre IE-Eigenschaft, viel besser, den W3C-Standard * textContent * zu verwenden. – RobG

+0

Ich löschte die zusätzlichen body-Tags und versuchte, den Wert in der Funktion calculate() zu eliminieren. Es funktioniert jedoch immer noch nicht. Ich verwende die div-Elemente, um das berechnete gpa aus der Berechnungsfunktion auszugeben. Wie kann ich die von den vier calculateGpa-Funktionen ausgegebenen Zahlenwerte erhalten und in die Calculate-Funktion eingeben? – Stephen

+0

@RobG Eigentlich wollte ich innerHTML verwenden, aber textContent ist besser als beide – xrgb