這是我第一次我動手的Java腳本。我盡我所能,結果被困在這裏:我的JavaScript代碼有錯誤
這是我的輸出。我不明白爲什麼我在最後得到未定義的值。也正因爲如此,最後的結果不來正確:
COURSE GRADE CREDIT-HOURS
Math A 4
Scie A 3
undefined undefined
Total Grade points = NaN
Number of hours = undefined43
GPA = NaN
<p id="demo"></p>
<script>
var ch, gp, gpa;
var grade;
document.write("COURSE GRADE CREDIT-HOURS ");
document.write("<hr>");
document.write("<hr>");
do {
grade = prompt("Enter the score in this format : <subject name><space><Grade><space><credit>");
var pro = grade.split(" ")[0];
var mid = grade.split(" ")[1];
var pre = grade.split(" ")[2];
document.write(pro);
document.write("          ");
document.write(mid);
document.write("          ");
document.write(pre);
document.write("<br>");
switch (mid) {
case "A":
gp = gp + (pre * 4)
ch = ch + pre;
break;
case "B+":
gp = gp + (pre * 3.5)
ch = ch + pre;
break;
case "B":
gp = gp + (pre * 3)
ch = ch + pre;
break;
case "C+":
gp = gp + (pre * 2.5)
ch = ch + pre;
break;
case "C":
gp = gp + (pre * 2)
ch = ch + pre;
break;
case "D":
gp = gp + (pre * 1)
ch = ch + pre;
break;
case "F":
gp = gp + (pre * 0)
ch = ch + pre;
break;
}
} while (grade != "");
document.write("Total Grade points = " + gp);
document.write("<br>");
document.write("Number of hours = " + ch);
document.write("<br>");
document.write("GPA = " + (gp/ch));
document.write("<br>");
</script>
提供此工作plunkr或的jsfiddle。僅僅通過查看這個死代碼就很難理解。 –
也許你應該爲0'變種CH = 0初始化CH;' –
也許添加警報(調試),這樣你可以評估,因爲它去一起。 –