制作一个显示学生成绩表的程序。教师根据作业数量( g <= assignments; 在循环中)从提示输入成绩。所以我正在制作它,以便将成绩平均在一起并显示一个字母成绩。现在,我收到一个错误,即“未定义分数”。不知道为什么。JSFiddle 说它完全有效,我不知道出了什么问题。
更新 谢谢大家,var score = 0; 修复它......但我的循环现在只能工作一次,即使我指定它循环 5 次。
这是我的小提琴:http: //jsfiddle.net/ndSMa/
// second table row begin
var rowtwo = document.createElement("tr");
var student = document.createElement("td");
var studentprompt = prompt("What is the student's name?");
var studenttext = document.createTextNode(studentprompt);
student.appendChild(studenttext);
rowtwo.appendChild(student);
for (var g = 1; g <= assignments; g++) {
while (!grade) {
var grade = prompt("Please enter the student score for assignment" + g + ".");
var gradecolumn = document.createElement("td");
var gradetext = document.createTextNode(grade);
gradecolumn.appendChild(gradetext);
rowtwo.appendChild(gradecolumn);
if (parseInt(grade, 10)) {
// accumulate score
score += parseInt(grade, 10);
var totalscore = score/assignments;
}
// grade is not a number
else grade = undefined;
}
if (score >= 94) {
var acolumn = document.createElement("td");
var atext = document.createTextNode("A");
totalcolumn.appendChild(atext);
rowtwo.appendChild(acolumn);
} else if (score >= 90) {
var aminuscolumn = document.createElement("td");
var aminustext = document.createTextNode("A-");
totalcolumn.appendChild(aminustext);
rowtwo.appendChild(aminuscolumn);
} else if (score >= 87) {
var bpluscolumn = document.createElement("td");
var bplustext = document.createTextNode("B+");
totalcolumn.appendChild(bplustext);
rowtwo.appendChild(bpluscolumn);
} else if (score >= 84) {
var bcolumn = document.createElement("td");
var btext = document.createTextNode("B");
totalcolumn.appendChild(btext);
rowtwo.appendChild(bcolumn);
} else if (score >= 80) {
var bminuscolumn = document.createElement("td");
var bminustext = document.createTextNode("B-");
totalcolumn.appendChild(bminustext);
rowtwo.appendChild(bminuscolumn);
} else if (score >= 77) {
var cpluscolumn = document.createElement("td");
var cplustext = document.createTextNode("C+");
totalcolumn.appendChild(cplustext);
rowtwo.appendChild(cpluscolumn);
} else if (score >= 74) {
var ccolumn = document.createElement("td");
var ctext = document.createTextNode("C");
totalcolumn.appendChild(ctext);
rowtwo.appendChild(ccolumn);
} else if (score >= 70) {
var cminuscolumn = document.createElement("td");
var cminustext = document.createTextNode("C-");
totalcolumn.appendChild(cminustext);
rowtwo.appendChild(cminuscolumn);
} else if (score >= 67) {
var dpluscolumn = document.createElement("td");
var dplustext = document.createTextNode("D+");
totalcolumn.appendChild(dplustext);
rowtwo.appendChild(dpluscolumn);
} else if (score >= 64) {
var dcolumn = document.createElement("td");
var dtext = document.createTextNode("D");
totalcolumn.appendChild(dtext);
rowtwo.appendChild(dcolumn);
} else if (score >= 60) {
var dminuscolumn = document.createElement("td");
var dminustext = document.createTextNode("D-");
totalcolumn.appendChild(dminustext);
rowtwo.appendChild(dminuscolumn);
} else if (score < 60) {
var fcolumn = document.createElement("td");
var ftext = document.createTextNode("F");
totalcolumn.appendChild(ftext);
rowtwo.appendChild(fcolumn);
}
}
tableBody.appendChild(rowtwo);