我有一系列数组作为单独的问题分数,然后是一个数组,根据问题的分数将分数添加到主分数中:
var totscore = 0
var subscore = 0
var _scores7 = [2, 1, 0];
var _scores8 = [3, 2, 1, 0];
var _scores9 = [3, 2, 1, 0];
var _scores10 = [2, 1, 0];
var _scores11 = [1, 0];
var _scores12 = [2, 1, 0];
var _scores13 = [2, 1, 0];
var subScores = [0, 1, 2, 3, 4, 5, 6];
然后,问题应该根据单选按钮上的答案加分:
if(document.getElementById("q7a").checked){
subscore += _scores7[0];
} else if(document.getElementById("q7b").checked){
subscore += _scores7[1];
} else if(document.getElementById("q7c").checked){
subscore += _scores7[2];
} else alert("Question seven has not been finished.");
if(document.getElementById("q8a").checked){
subscore += _scores8[0];
} else if(document.getElementById("q8b").checked){
subscore += _scores8[1];
} else if(document.getElementById("q8c").checked){
subscore += _scores8[2];
} else if(document.getElementById("q8d").checked){
subscore += _scores8[3];
} else alert("Question eight has not been finished.");
if(document.getElementById("q9a").checked){
subscore += _scores9[0];
} else if(document.getElementById("q9b").checked){
subscore += _scores9[1];
} else if(document.getElementById("q9c").checked){
subscore += _scores9[2];
} else if(document.getElementById("q9d").checked){
subscore += _scores9[3];
} else alert("Question nine has not been finished.");
if(document.getElementById("q10a").checked){
subscore += _scores10[0];
} else if(document.getElementById("q10b").checked){
subscore += _scores10[1];
} else if(document.getElementById("q10c").checked){
subscore += _scores10[2];
} else alert("Question ten has not been finished.");
if(document.getElementById("q11a").checked){
subscore += _scores11[0];
} else if(document.getElementById("q11b").checked){
subscore += _scores11[1];
} else alert("Question eleven has not been finished.");
if(document.getElementById("q12a").checked){
subscore += _scores12[0];
} else if(document.getElementById("q12b").checked){
subscore += _scores12[1];
} else if(document.getElementById("q12c").checked){
subscore += _scores12[2];
} else alert("Question twelve has not been finished.");
if(document.getElementById("q13a").checked){
subscore += _scores13[0];
} else if(document.getElementById("q13b").checked){
subscore += _scores13[1];
} else if(document.getElementById("q13c").checked){
subscore += _scores13[2];
} else alert("Question thirteen has not been finished.");
此处收到的分数应该在以下代码中使用:
switch(true){
case (subscore == 0):
totscore += subScores[6];
break;
case (subscore >= 1 && subscore <= 3):
totscore += subScores[5];
break;
case (subscore >= 4 && subscore <= 6):
totscore += subScores[4];
break;
case (subscore >= 7 && subscore <= 8):
totscore += subScores[3];
break;
case (subscore >= 9 && subscore <= 11):
totscore += subScores[2];
break;
case (subscore >= 12 && subscore <= 14):
totscore += subScores[1];
break;
case (subscore == 15):
totscore += subScores[0];
break;
default:
alert(subscore);
我的主要问题似乎是子核添加不会在我想要的地方结束。
当您选择应该给您零分核心分数的答案时,您最终得到六分。当你回答那些应该给你十五分的问题时,你会得到零。如果你在中间回答,你最终得到三到四个。
答案 0 :(得分:0)
我想出了自己的错误。
最后的switch语句的默认设置为:
alert(subscore)
switch语句之外的subscore没有实际警报。所以函数实际上是正确计算totscore,我误读了它作为subscore值的警告。
代码实际上是正确的,当我的错误显示答案时,我在计算中寻找错误。