如何跟踪用户的正确答案并在最后以javascript显示?

时间:2017-01-16 23:41:58

标签: javascript

我对javascript知之甚少,我正在进行javascript测验,其中我的问题显示在提示中。我需要我的测验来跟踪用户已正确回答的答案数量,并在游戏结束时输出这个答案以及正确答案的百分比。我看了我的问题,我添加了函数returnScore(){       警报("你的分数为" + getScore()+" /" + tot);     } 我的编码,它不起作用。

`<!DOCTYPE html>
<html>
<head>
<title>Supernatural Trvia Quiz</title>
<style type="text/css">
body{
    background-image : url("http://wallpapercave.com/wp/tNqlnzg.jpg");
    background-repeat : no-repeat;
    background-size: 100% 100%;    
    position:absolute; 
    margin-left: 5%;
    margin-top: 5%;
    color:white;
    font-size:20px;
    font-family:Georgia;
    font-weight: bold;
    }
</style>
<script>

i = 1 // counter variable

 var score=0
function main(){  // main part of program
if (i==1){ 
    document.view.qnum.value=i;  // puts question # in text box
    document.view.question.value ="What are the names of the main characters?\na) Sam and Bobby\nb) Sam and Dean\nc) Jess and Dean\nd) Sam and John ";

    // above code puts the question in the textarea, note the use of \n for "new line"

    correct="b"  // assigns the correct answer

    ans=prompt(i+'.) What are the names of the main characters? \na) Sam and Bobby\nb) Sam and Dean\nc) Jess and Dean\nd) Sam and John')  // gives user a chance to answer

}// end of question 1

else if (i==2){ 
    document.view.qnum.value=i;  // puts question # in text box
    document.view.question.value ="In the first episode when Sam got home, where did he find his girlfriend dead?\na) On the ceiling\nb) On the floor\nc) In bed\nd) In the kitchen ";

    // above code puts the question in the textarea, note the use of \n for "new line"

    correct="a"  // assigns the correct answer

    ans=prompt(i+'.) In the first episode when Sam got home, where did he find his girlfriend dead?\na) On the ceiling\nb) On the floor\nc) In bed\nd) In the kitchen ')  // gives user a chance to answer

}// end of question 2

else if (i==3){ 
    document.view.qnum.value=i;  // puts question # in text box
    document.view.question.value ="What is the name of Sam and Dean's father? \na) John Winchester \nb) Adam Winchester \nc) Castiel Novak \nd) Bobby Singer";

    // above code puts the question in the textarea, note the use of \n for "new line"

    correct="a"  // assigns the correct answer

    ans=prompt(i+'.) What is the name of Sam and Dean s father? \na) John Winchester \nb) Adam Winchester \nc) Castiel Novak \nd) Bobby Singer ')  // gives user a chance to answer

}// end of question 3

else if (i==4){ 
    document.view.qnum.value=i;  // puts question # in text box
    document.view.question.value ="What is Dean's favourite dessert? \na) Cake \nb) Eclair \nc) Ice cream \nd) Pie";

    // above code puts the question in the textarea, note the use of \n for "new line"

    correct="d"  // assigns the correct answer

    ans=prompt(i+'.) What is Dean s favourite dessert? \na) Cake \nb) Eclair \nc) Ice cream \nd) Pie ')  // gives user a chance to answer

}// end of question 4


else if (i==5){ 
    document.view.qnum.value=i;  // puts question # in text box
    document.view.question.value ="Who was Sam dating in the beginning of season 1? \na) Meg \nb) Madison \nc) Jessica \nd) Ruby";

    // above code puts the question in the textarea, note the use of \n for "new line"

    correct="c"  // assigns the correct answer

    ans=prompt(i+'.) Who was Sam dating in the beginning of season 1? \na) Meg \nb) Madison \nc) Jessica \nd) Ruby ')  // gives user a chance to answer

}// end of question 5

else if (i==6){ 
    document.view.qnum.value=i;  // puts question # in text box
    document.view.question.value ="In the pilot episode, who or what did the boys search for? \na) Windigo \nb) Bloody Mary \nc) Dracula \nd) The Woman in White";

    // above code puts the question in the textarea, note the use of \n for "new line"

    correct="d"  // assigns the correct answer

    ans=prompt(i+'.) In the pilot episode, who or what did the boys search for? \na) Windigo \nb) Bloody Mary \nc) Dracula \nd) The Woman in White ')  // gives user a chance to answer

}// end of question 6

else if (i==7){ 
    document.view.qnum.value=i;  // puts question # in text box
    document.view.question.value ="What is the license plate on the Impala during seasons 1 and 2? \na) KAZ-2Y5 \nb) K4Z-2Y8 \nc) KA2-ZY5 \nd) KA2-2Y9";

    // above code puts the question in the textarea, note the use of \n for "new line"

    correct="a"  // assigns the correct answer

    ans=prompt(i+'.) What is the license plate on the Impala during seasons 1 and 2? \na) KAZ-2Y5 \nb) K4Z-2Y8 \nc) KA2-ZY5 \nd) KA2-2Y9 ')  // gives user a chance to answer

}// end of question 7

else if (i==8){ 
    document.view.qnum.value=i;  // puts question # in text box
    document.view.question.value ="When is Dean's birthday? \na) July 24, 1979 \nb) June 20, 1980 \nc) January 16, 1980 \nd) January 24, 1979";

    // above code puts the question in the textarea, note the use of \n for "new line"

    correct="d"  // assigns the correct answer

    ans=prompt(i+'.) When is Dean s birthday? \na) July 24, 1979 \nb) June 20, 1980 \nc) January 16, 1980 \nd) January 24, 1979 ')  // gives user a chance to answer

}// end of question 8

else if (i==9){ 
    document.view.qnum.value=i;  // puts question # in text box
    document.view.question.value ="Who is the king of Hell? \na)  Zachariah \nb) Crowley \nc) Castiel \nd) Raphael";

    // above code puts the question in the textarea, note the use of \n for "new line"

    correct="b"  // assigns the correct answer

    ans=prompt(i+'.) Who is the king of Hell? \na)  Zachariah \nb) Crowley \nc) Castiel \nd) Raphael ')  // gives user a chance to answer

}// end of question 9

else if (i==10){ 
    document.view.qnum.value=i;  // puts question # in text box
    document.view.question.value ="What is the name of Sam and Dean's brother? \na)  Adam \nb) Kevin \nc) Castiel \nd) Bobby";

    // above code puts the question in the textarea, note the use of \n for "new line"

    correct="a"  // assigns the correct answer

    ans=prompt(i+'.) What is the name of Sam and Dean s brother? \na)  Adam \nb) Kevin \nc) Castiel \nd) Bobby ')  // gives user a chance to answer

}// end of question 10

else if (i==11){ 
    document.view.qnum.value=i;  // puts question # in text box
    document.view.question.value ="What is the name of the dogs? \na) Demon Dogs \nb) Hell Hounds \nc) Ghostfacers \nd) Dogs";

    // above code puts the question in the textarea, note the use of \n for "new line"

    correct="b"  // assigns the correct answer

    ans=prompt(i+'.) What is the name of the dogs? \na) Demon Dogs \nb) Hell Hounds \nc) Ghostfacers \nd) Dogs ')  // gives user a chance to answer

}// end of question 11

else if (i==12){ 
    document.view.qnum.value=i;  // puts question # in text box
    document.view.question.value ="Who Killed Sam & Dean's mother? \na) Crowley \nb) A demon \nc) Lucifer \nd) The yellowed eyed demon";

    // above code puts the question in the textarea, note the use of \n for "new line"

    correct="d"  // assigns the correct answer

    ans=prompt(i+'.) Who Killed Sam & Dean s mother? \na) Crowley \nb) A demon \nc) Lucifer \nd) The yellowed eyed demon ')  // gives user a chance to answer

}// end of question 12

else if (i==13){ 
    document.view.qnum.value=i;  // puts question # in text box
    document.view.question.value ="Who wants Sam's vessel? \na) Crowley \nb) Lucifer \nc) Raphael \nd) Zachariah";

    // above code puts the question in the textarea, note the use of \n for "new line"

    correct="b"  // assigns the correct answer

    ans=prompt(i+'.) Who wants Sam s vessel? \na) Crowley \nb) Lucifer \nc) Raphael \nd) Zachariah ')  // gives user a chance to answer

}// end of question 13

else if (i==14){ 
    document.view.qnum.value=i;  // puts question # in text box
    document.view.question.value ="Which actor plays Sam Winchester? \na) Jensen Ackles \nb) Mark Sheppard \nc) Jared Padalecki \nd) Osric Chau";

    // above code puts the question in the textarea, note the use of \n for "new line"

    correct="c"  // assigns the correct answer

    ans=prompt(i+'.) Which actor plays Sam Winchester? \na) Jensen Ackles \nb) Mark Sheppard \nc) Jared Padalecki \nd) Osric Chau ')  // gives user a chance to answer

}// end of question 14

else if (i==15){ 
    document.view.qnum.value=i;  // puts question # in text box
    document.view.question.value ="Which Winchester was the first to die? \na) Mary \nb) John \nc) Sam \nd) Dean";

    // above code puts the question in the textarea, note the use of \n for "new line"

    correct="a"  // assigns the correct answer

    ans=prompt(i+'.) Which Winchester was the first to die? \na) Mary \nb) John \nc) Sam \nd) Dean ')  // gives user a chance to answer

}// end of question 15

else if (i==16){ 
    document.view.qnum.value=i;  // puts question # in text box
    document.view.question.value ="What chemical compound is one of the few weaknesses of Leviathans? \na) Nitrate \nb) Borax \nc) Sulfur \nd) Ferrate";

    // above code puts the question in the textarea, note the use of \n for "new line"

    correct="b"  // assigns the correct answer

    ans=prompt(i+'.) What chemical compound is one of the few weaknesses of Leviathans? \na) Nitrate \nb) Borax \nc) Sulfur \nd) Ferrate ')  // gives user a chance to answer

}// end of question 16

else if (i==17){ 
    document.view.qnum.value=i;  // puts question # in text box
    document.view.question.value ="What chemical compound is one of the few weaknesses of Leviathans? \na) 1 \nb) 2 \nc) 3 \nd) 4";

    // above code puts the question in the textarea, note the use of \n for "new line"

    correct="c"  // assigns the correct answer

    ans=prompt(i+'.) What chemical compound is one of the few weaknesses of Leviathans? \na) 1 \nb) 2 \nc) 3 \nd) 4 ')  // gives user a chance to answer

}// end of question 17


else if (i==18){ 
    document.view.qnum.value=i;  // puts question # in text box
    document.view.question.value ="The boys have had numerous runins with a group of self-proclaimed paranormal experts who call themselves what? \na) Ghostblasters \nb) Ghostkillers \nc) Ghosthunters \nd) Ghostfacers";

    // above code puts the question in the textarea, note the use of \n for "new line"

    correct="d"  // assigns the correct answer

    ans=prompt(i+'.) The boys have had numerous runins with a group of self-proclaimed paranormal experts who call themselves what? \na) Ghostblasters \nb) Ghostkillers \nc) Ghosthunters \nd) Ghostfacers ')  // gives user a chance to answer

}// end of question 18

else if (i==19){ 
    document.view.qnum.value=i;  // puts question # in text box
    document.view.question.value ="After whom was Dean Winchester named? \na) His grandfather \nb) His grandmother \nc) His father \nd) No one";

    // above code puts the question in the textarea, note the use of \n for "new line"

    correct="b"  // assigns the correct answer

    ans=prompt(i+'.) After whom was Dean Winchester named? \na) His grandfather \nb) His grandmother \nc) His father \nd) No one ')  // gives user a chance to answer

}// end of question 19

else if (i==20){ 
    document.view.qnum.value=i;  // puts question # in text box
    document.view.question.value ="Which actor plays the character Castiel Novak?\na) Misha Collins \nb) Jim Beaver \nc) Mark Pellegrino \nd) Mark Sheppard";

    // above code puts the question in the textarea, note the use of \n for "new line"

    correct="a"  // assigns the correct answer

    ans=prompt(i+'.) Which actor plays the character Castiel Novak?\na) Misha Collins \nb) Jim Beaver \nc) Mark Pellegrino \nd) Mark Sheppard ')  // gives user a chance to answer

}// end of question 20



    document.view.go.focus();  // puts the focus on the "Next Question >" button so the enter key will activate it

    if (correct==ans){  // compares the real answer to the users answer

        document.view.question.value =" You are right!" // puts comment in textarea
score ++;       
    }

    else{
        document.view.question.value =" You are wrong" // puts comment in textarea

    }


    i++; // keeps track of number of questions asked

function returnScore(){
  alert("Your score is "+ getScore() +"/"+ tot);
}


} //end of main function


</script>
</head>
<body LINK="F7DD5E" VLINK="F7DD5E" ALINK="F7DD5E" onLoad="main()">

<FORM name="view">  
    Question: &nbsp;<input type="text" name="qnum" size="2">&nbsp;&nbsp;<input type="button" name="go" value="Next Question >" onClick=main()>
    <br><br>
    <TEXTAREA COLS=36 ROWS=9 name="question" wrap="virtual"></TEXTAREA> 
</FORM> 

</body>
</html>
`

1 个答案:

答案 0 :(得分:0)

你非常接近。增加i后,您需要检查它是否大于20,因为您总共有20个问题,然后显示分数。而不是:

function returnScore(){
  alert("Your score is "+ getScore() +"/"+ tot);
}

把这个:

// i is not 1 to 20, so there are no more questions
if (i > 20){

    alert("End of Game\nYour score is: "+ score + "/" + 20 + "\nYou scored: " + (score / 20) * 100 + "%" );
}