简体   繁体   English

我的JavaScript代码有错误

[英]My JavaScript code has an error

This is my first my hands-on with Java script. 这是我第一次动手使用Java脚本。 I tried my best and got stuck here: 我尽力了,被困在这里:

This is my output. 这是我的输出。 I don't understand why I'm getting the undefined values in the last. 我不明白为什么我最后要得到未定义的值。 Also because of this, final results are not coming properly: 也是因为这个原因,最终结果无法正确显示:

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> 

Initialize variables. 初始化变量。

var gp = 0;
var ch = 0;

The code is getting executed once after the null input. 空输入后,代码将被执行一次。 Add a break statement 添加中断声明

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

after

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

Edit: Also surround document.write("GPA = " + (gp / ch)); 编辑:还环绕document.write("GPA = " + (gp / ch)); to handle case when ch = 0 . ch = 0时处理大小写。 For ex- 对于前

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

Please study this example. 请研究这个例子。

Note the parseInt and test that the divisor is > 0 注意parseInt并测试除数> 0

Also note I replaced the document.write with an innerHTML of demo as I suspect you want - if you use document.write, you cannot run the code in a function after the page has loaded 还要注意,我怀疑自己想用演示的innerHTML替换了document.write-如果使用document.write,则在页面加载后无法在函数中运行代码

 <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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM