2017-06-27 49 views
0

這是我第一次我動手的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("&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp"); 
 
    document.write(mid); 
 
    document.write("&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp"); 
 
    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>

+0

提供此工作plunkr或的jsfiddle。僅僅通過查看這個死代碼就很難理解。 –

+1

也許你應該爲0'變種CH = 0初始化CH;' –

+0

也許添加警報(調試),這樣你可以評估,因爲它去一起。 –

回答

0

初始化變量。

var gp = 0; 
var ch = 0; 

該代碼在null輸入後執行一次。 添加break語句

if (grade == "") {break;} 

grade = prompt("Enter the..."); 

編輯: 還圍繞document.write("GPA = " + (gp/ch));ch = 0處理情況。 For ex-

if(ch != 0) { 
    document.write("GPA = " + (gp/ch)); 
    document.write("<br>"); 
} 
+0

這還不夠。然後所有的計算結果將是NaN – mplungjan

+0

你試過了嗎? – rushi

+0

是的,我有。你有嗎? – mplungjan

0

請研究一下這個例子。

注parseInt函數和測試除數爲> 0

另外請注意我替換文件撰寫與演示的innerHTML的,因爲我懷疑你想要的 - 如果你使用文件撰寫,則不能運行代碼在功能後,頁面加載

<p id="demo"></p> 
 
<script> 
 
    var ch=0, gp=0, gpa=0, grade, 
 
    html = "COURSE GRADE CREDIT-HOURS <hr><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]; 
 
    if (
 
     isNaN(pre) || pre.trim() =="" 
 
    ) { 
 
     break; // stop 
 
    } 
 
    pre = parseInt(pre,10); // make it a number 
 
    mid = mid=="" ? "":mid.toUpperCase(); // your switch is case sensitive 
 
    html += pro; 
 
    html += "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp"; 
 
    html += mid; 
 
    html += "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp"; 
 
    html += pre; 
 
    html += "<br>"; 
 

 
    ch += pre; // always 
 

 
    switch (mid) { 
 
     case "A": 
 
     gp += (pre * 4 ); break; 
 
     case "B+": 
 
     gp += (pre * 3.5); break; 
 
     case "B": 
 
     gp += (pre * 3 ); break; 
 
     case "C+": 
 
     gp += (pre * 2.5); break; 
 
     case "C": 
 
     gp += (pre * 2 ); break; 
 
     case "D": 
 
     gp += (pre * 1 ); break; 
 
     case "F": 
 
     gp += (pre * 0 ); break; 
 
    } 
 

 
    } while (grade != ""); 
 
html += "Total Grade points = " + gp; 
 
html += "<br>"; 
 
html += "Number of hours = " + ch; 
 
html += "<br>"; 
 
if (gp > 0 && ch > 0) html += "GPA = " + (gp/ch).toFixed(2); 
 
document.getElementById("demo").innerHTML=html; // now it can be in a function too 
 
</script>

+0

謝謝。現在工作正常。! –