Jack Nilan         Javascript Tutorial        EMail : jacknilan@yahoo.com



Lesson 10 - Creating a Quiz with JavaScript

<html>
<SCRIPT>
questions = new Array (2);
ans1 = new Array(2);
ans2 = new Array(2);
ans3 = new Array(2);
ca = new Array(2);
questions[0] = "Who was first president?";
ans1[0] = "a. Washington";
ans2[0] ="b. Lincoln";
ans3[0] ="c. Jefferson";
ca[0] ="a";
questions[1] = "Who wrote Huckleberry Finn?";
ans1[1] = "a. Poe";
ans2[1] ="b. Twain";
ans3[1] ="c. Hemingway";
ca[1] ="b";
var ct = 0;
var correct = 0;
var wrong = 0;


function fillboxes()
{if (ct < questions.length)
{
document.quiz.question.value = questions[ct];
document.quiz.ans1.value = ans1[ct];
document.quiz.ans2.value = ans2[ct];
document.quiz.ans3.value = ans3[ct];
document.quiz.resultbox.value ="";
document.quiz.answer.value ="";
}
else
{document.quiz.resultbox.value = "Final Grade " + correct/(correct+wrong)*100;
}
}


function checkanswer(ans)
{if (ans == ca[ct])
{ document.quiz.resultbox.value = "Correct";
correct++;
}
else
{document.quiz.resultbox.value = "Wrong, correct answer was " + ca[ct];
wrong++;
}
ct++;
}
</SCRIPT>

<BODY onLoad ="fillboxes();">
<FORM name=quiz>
<INPUT size=80 name=question> <br>
<INPUT size=80 name=ans1> <br>
<INPUT size=80 name=ans2> <br>
<INPUT size=80 name=ans3> <br>
<br> <INPUT size=5 name=answer> <br> <INPUT onclick=checkanswer(document.quiz.answer.value) type=button value="Type in answer and Click" name=button1>
<INPUT onclick="fillboxes();" type=button value="Next Question" name=button2>
<BR>
<BR>
<INPUT size=45 name=resultbox>
</FORM>
</BODY>
</html>