JavaScript多项选择测验中的未知错误

时间:2016-06-01 18:26:46

标签: javascript arrays multiple-choice

我试图将它作为基本的,因为我给出的约束绝对可能。分数必须在数组中,年龄和BMI必须有两个数字输入。

在HTML代码中,除了年龄和BMI问题之外,它是所有无线电元素。那里没有错误,只有在我尝试使用此脚本运行时才会出现错误。

function getScore(){

var totscore = 0;
var subscore = 0;

var scores1 = [0, 2, 4];
var scores2 = [0, 1, 2, 3, 4];
var scores3 = [0, 1, 2, 3, 4];
var scores4 = [0, 1, 2];

var scoresAge = [0, 1, 2, 3, 4, 5];
var scoresBMI = [0, 2, 4];

var scores6 = [0, 1, 2];

var scores7 = [2, 1, 0];
var scores8 = [2, 1, 0];
var scores9 = [3, 2, 1, 0];
var scores10 = [3, 2, 1, 0];
var scores11 = [2, 1, 0];
var scores12 = [1, 0];
var scores13 = [2, 1, 0];


var subScores = [0, 1, 2, 3, 4, 5, 6];

var scores14 = [0, 1, 2];


//QUESTION 1
if(document.getElementById("q1a").checked){
    totscore = totscore + scores1[0];
} else if(document.getElementById("q1b").checked){
    totscore = totscore + scores1[1];
} else if(document.getElementById("q1c").checked){
    totscore = totscore + scores1[2];
} else alert ("Question one has not been finished.");

//QUESTION 2
if(document.getElementById("q2a").checked){
    totscore = totscore + scores2[0];
} else if(document.getElementById("q2b").checked){
    totscore = totscore + scores2[1];
} else if(document.getElementById("q2c").checked){
    totscore = totscore + scores2[2];
} else if(document.getElementById("q2d").checked){
    totscore = totscore + scores2[3];
} else if(document.getElementById("q2e").checked){
    totscore = totscore + scores2[4];
} else alert("Question two has not been finished.");

//QUESTION 3
if(document.getElementById("q3a").checked){
    totscore = totscore + scores3[0];
} else if(document.getElementById("q3b").checked){
    totscore = totscore + scores3[1];
} else if(document.getElementById("q3c").checked){
    totscore = totscore + scores3[2];
} else if(document.getElementById("q3d").checked){
    totscore = totscore + scores3[3];
} else if(document.getElementById("q3e").checked){
    totscore = totscore + scores3[4];
} else alert("Question three has not been finished.");

//QUESTION 4
if(document.getElementById("q4a").checked){
    totscore = totscore + scores4[0];
} else if(document.getElementById("q4b").checked){
    totscore = totscore + scores4[1];
} else if(document.getElementById("q4c").checked){
    totscore = totscore + scores4[2];
} else alert("Question four has not been finished.");

//AGE QUESTION
var yr = document.getElementById("_age");

if (document.getElementById("_age").isNaN){
    alert("Please input a valid age.");
} else if (yr <= 0){
    alert("Please input a valid age.");
} else if(yr < 55){
    totscore = totscore + scoresAge[5];
} else if(55 <= yr < 65){
    totscore = totscore + scoresAge[4];
} else if(65 <= yr < 70){
    totscore = totscore + scoresAge[3];
} else if(70 <= yr < 75){
    totscore = totscore + scoresAge[2];
} else if(75 <= yr < 80){
    totscore =  totscore + scoresAge[1];
} else if(80 <= yr){
    totscore = totscore + scoresAge[0];
} else alert("Please input a valid age.");

//BMI QUESTION
var ht = document.getElementById("_height");
var wt = document.getElementById("_weight");

if (document.getElementById("_height").isNaN){
    alert("Please input a valid height in inches.");
}
if (document.getElementById("_weight").isNaN){
    alert("Please input a valid weight in pounds.");
}

var bmi = (wt*703)/(ht*ht);

if(bmi <=0){
    alert("Please make sure your height and weight inputs are correct.");
} else if(bmi <= 30){
    totscore = totscore + scoresBMI[2];
} else if(30 < bmi <= 37){
    totscore = totscore + scoresBMI[1];
} else if(37 < bmi){
    totscore = totscore + scoresBMI[0];
} else if(200 <= bmi){
    alert("Please make sure your height and weight inputs are correct.");
}

//QUESTION 6
if(document.getElementById("q6a").checked){
    totscore = totscore + scores6[0];
} else if(document.getElementById("q6b").checked){
    totscore = totscore + scores6[1];
} else if(document.getElementById("q6c").checked){
    totscore = totscore + scores6[2];
} else alert("Question six has not been finished.");

//SUBQUIZ BEGINS

//QUESTION 7
if(document.getElementById("q7a").checked){
    subscore = subscore + scores7[0];
} else if(document.getElementById("q7b").checked){
    subscore = subscore + scores7[1];
} else if(document.getElementById("q7c").checked){
    subscore = subscore + scores7[2];
} else alert("Question seven has not been finished.");

//QUESTION 8
if(document.getElementById("q8a").checked){
    subscore = subscore + scores8[0];
} else if(document.getElementById("q8b").checked){
    subscore = subscore + scores8[1];
} else if(document.getElementById("q8c").checked){
    subscore = subscore + scores8[2];
} else if(document.getElementById("q8d").checked){
    subscore = subscore + scores8[3];
} else alert("Question eight has not been finished.");

//QUESTION 9
if(document.getElementById("q9a").checked){
    subscore = subscore + scores9[0];
} else if(document.getElementById("q9b").checked){
    subscore = subscore + scores9[1];
} else if(document.getElementById("q9c").checked){
    subscore = subscore + scores9[2];
} else if(document.getElementById("q9d").checked){
    subscore = subscore + scores9[3];
} else alert("Question nine has not been finished.");

//QUESTION 10
if(document.getElementById("q10a").checked){
    subscore = subscore + scores10[0];
} else if(document.getElementById("q10b").checked){
    subscore = subscore + scores10[1];
} else if(document.getElementById("q10c").checked){
    subscore = subscore + scores10[2];
} else alert("Question ten has not been finished.");

//QUESTION 11
if(document.getElementById("q11a").checked){
    subscore = subscore + scores11[0];
} else if(document.getElementById("q11b").checked){
    subscore = subscore + scores11[1];
} else alert("Question eleven has not been finished.");

//QUESTION 12
if(document.getElementById("q12a").checked){
    subscore = subscore + scores12[0];
} else if(document.getElementById("q12b").checked){
    subscore = subscore + scores12[1];
} else if(document.getElementById("q12c").checked){
    subscore = subscore + scores12[2];
} else alert("Question twelve has not been finished.");

//QUESTION 13
if(document.getElementById("q13a").checked){
    subscore = subscore + scores13[0];
} else if(document.getElementById("q13b").checked){
    subscore = subscore + scores13[1];
} else if(document.getElementById("q13c").checked){
    subscore = subscore + scores13[2];
} else alert("Question thirteen has not been finished.");

//SUBQUIZ CALCULATION
if(subscore < 0){
    alert("Please make sure you have answered questions seven through thirteen.");
} else if(subscore = 0){
    totscore = totscore + subScores[6];
} else if(1 <= subscore <= 3){
    totscore = totscore + subScores[5];
} else if(4 <= subscore <= 6){
    totscore = totscore + subScores[4];
} else if(7 <= subscore <= 8){
    totscore = totscore + subScores[3];
} else if(9 <= subscore <= 11){
    totscore = totscore + subScores[2];
} else if(12 <= subscore <= 14){
    totscore = totscore + subScores[1];
} else if(subscore = 15){
    totscore = totscore + subScores[0];
} else if(subscore> 15)
    alert("An error has occured. Please restart and try again.");

//SUBQUIZ ENDS

//QUESTION 14
if(document.getElementById("q14a").checked){
    totscore = totscore + scores14[0];
} else if(document.getElementById("q14b").checked){
    totscore = totscore + scores14[1];
} else if(document.getElementById("q14c").checked){
    totscore = totscore + scores14[2];
} else alert("Question fourteen has not been finished.");

alert(totscore)
}

1 个答案:

答案 0 :(得分:-1)

要减少编码,请使用JavaScript Assignment Operators。

x + = y(与说:x = x + y相同)

所以而不是:

totscore = totscore + scores1[0];

它可以是:

totscore += scores1[0];

对于您的年龄问题,请确保其获得该对象的价值:

//AGE QUESTION
var yr = document.getElementById("_age");

应该是:

var yr = document.getElementById("_age").value;

与您提取的其他元素字段相同。