Wrox Programmer Forums
|
BOOK: Beginning JavaScript
This is the forum to discuss the Wrox book Beginning JavaScript by Paul Wilton; ISBN: 9780764544057
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning JavaScript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 23rd, 2007, 05:47 PM
Registered User
 
Join Date: Apr 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Trivia Quiz ch6

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?






Similar Threads
Thread Thread Starter Forum Replies Last Post
Anybody find the missing code in ch6? nickolas BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 1 January 29th, 2006 10:38 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.