A simple quiz
Test your knowledge of pinnipeds!
This example uses the onClick handlers in the radio buttons to pass the user's
choices to the program. Using the this.value construct simplifies the nomenclature
of radio buttons in a document. The button calls the Score() function, which
checks the user's answers against an answer key and totals the score. This example has been simplified;
the original provided explanations for the correct answers and editorialized on the user's expertise.
Code adapted from
this quiz on the
JavaScript Source site.
Code in the page header
<SCRIPT LANGUAGE="JavaScript">
<!--
var ans = new Array;
var yourAns = new Array("", "", "", "", "");
ans[1] = "b";
ans[2] = "a";
ans[3] = "c";
ans[4] = "d";
ans[5] = "b";
function Engine(question, answer) {
yourAns[question]=answer;
}
function Score(){
var score = 0 ;
var answerText = "How did you do?\n===============================";
for(i=1;i<=5;i++){
answerText=answerText+"\nQuestion "+i+": ";
if(ans[i]!=yourAns[i]){
answerText=answerText+" The correct answer was "+ans[i]+" \n";
}
else{
answerText=answerText+" Correct! \n";
score++;
}
}
answerText=answerText+"===============================\nYour total score is : "+score+" \n";
alert(answerText);
}
// -->
</script>
The form