Hey, I'm a noob to JavaScript and just recently started reading Begining JavaScript (really wish I could have gotten a more recent version, but it was the only one at the library). I am having trouble with the Trivia Quiz from Chapter 6 and can't find the problem. Here's a copy of the code
Code:
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Trivia Quiz</title>
<script type="text/javascript">
<!--
var questionNumber
function answerCorrect (questionNumber, answer){
var correct=false
if (answer==answers[questionNumber]){
correct=true
}
return correct
}
function getQuestion(){
questionNumber=Math.floor(Math.random()*(questions.length))
var questionHTML="<p>"+questions[questionNumber][0]+"</p>"
var questionLength=questions[questionNumber].length
var questionChoice
for (questionChoice=1; questionChoice<questionLength; questionChoice++){
questionHTML=questionHTML+"<input type=\"radio\" name=\"radQuestionChoice\""
if (questionChoice==1){
questionHTML=questionHTML+" checked=\"checked\""
}
questionHTML=questionHTML+" />"
questionHTML=questionHTML+questions[questionNumber][questionChoice]
questionHTML=questionHTML+"<br />"
}
document.QuestionForm.txtQNumber.value=questionNumber+1
}
function buttonCheckQ_onclick(){
var answer=0
while (document.QuestionForm.radQuestionChoice[answer].checked!=true){
answer++
}
answer=String.fromCharCode(65+answer)
if (answerCorrect(questionNumber,answer)==true){
alert("You got it right")
}
else {
alert("Wrong answer")
}
window.location.reload()
}
//-->
</script>
</head>
<body>
<script type="text/javascript">
<!--
var questions=new Array()
var answers=new Array()
questions[0]=new Array()
//Question 1
questions[0][0]="The beatles were"
questions[0][1]="a sixties rock group from Liverpool"
questions[0][2]="a bug that runs around on the ground and annoys people"
questions[0][3]="I don't know, can I have the questions on baseball please?"
questions[0][4]="a group of well-known insects"
//answer for question 1
answers[0]="A"
//Question 2
questions[1]=new Array()
questions[1][0]="Homer Simpson's favorite food is..."
questions[1][1]="fresh salad"
questions[1][2]="doughnuts"
questions[1][3]="bread and water"
questions[1][4]="apples"
//answer for question 2
answers[1]="B"
//Question 3
questions[2]=new Array()
questions[2][0]="Lisa Simpson plays which musical instrument?"
questions[2][1]="Clarinet"
questions[2][2]="Oboe"
questions[2][3]="Saxophone"
questions[2][4]="Tubular Bells"
//answer for question 3
answers[2]="C"
//-->
</script>
<form name="QuestionForm">
Question
<input type="text" name="txtQNumber" size="1" />
<script type="text/javascript">
<!--
document.write(getQuestion())
//-->
</script>
<input type="button" value="Check Question" name="buttonCheckQ" onclick="return buttonCheckQ_onclick()" />
</form>
</body>
</html>
Can someone help me find the problem?