Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning JavaScript 3rd Ed. ISBN: 978-0-470-05151-1
This is the forum to discuss the Wrox book Beginning JavaScript, 3rd Edition by Paul Wilton, Jeremy McPeak; ISBN: 9780470051511
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning JavaScript 3rd Ed. ISBN: 978-0-470-05151-1 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old May 2nd, 2009, 07:31 AM
Registered User
 
Join Date: May 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 6: Trivia Quiz

I'm trying to adapt the code for the trivia quiz from chapter 6 for my own quiz on my website.

However, on my quiz I have all of my questions set up on one page, rather than picking one at random. The problem is, the code produces no errors but returns every question option as wrong no matter whether I select the option that I have declared as correct or not.

Here's my code:

Code:
 
function answerCorrect(answer)
{
// declare a variable to hold return value
var correct = false;

// if answer provided is same as correct answer then correct variable is true
if (answer == answers)
correct = true;

// return whether the answer was correct (true or false)
return correct;
}


function buttonCheckQ_onclick() 
{
var answer = 0; // initialize variable answer to 0. This will hold the index of the answer radio button selected (taken from the array called answers)

while (document.frmQuiz.qOption[answer].checked != true) 
{
answer++; // increment through the answer radio buttons until the one that is checked is found
}
answer = String.fromCharCode(65 + answer); // convert the radio button index found by the while loop into a character (uses character codes, 65 is A so if user chooses button 0 then 0+65 = A)

if (answerCorrect(answer) == true)
{
alert("You got it right"); // if the index number of the answer matches the one held in the array answers, display an alert informing the user
}
else
{
alert("You got it wrong"); // if the index number of the selected radio button does not match the one held in array answers, display a different alert
}
window.location.reload(); // refresh the page once the user has clicked Check Answer 
}


// questions and answers arrays will holds questions and answers
var questions = new Array();
var answers = new Array();

questions[0] = new Array; // define question 1 as an array; as the questions are multiple choice so the array for the questions needs to be multi-dimensional

questions[0][0] = "When was ESP founded?"; // text for question 1

questions[0][1] = "1965"; // choices for answer set out as radio buttons on quiz form
questions[0][2] = "1995";
questions[0][3] = "1975";
questions[0][4] = "1985";

answers[0] = "C" // correct answer for question 1 (placed in answers array)


questions[1] = new Array;
// define text and answer for question 2
questions[1][0] = "Which US city did ESP move to from Japan?";

questions[1][1] = "New York";
questions[1][2] = "Los Angeles";
questions[1][3] = "San Francisco";
questions[1][4] = "Houston";

answers[1] = "A";


questions[2] = new Array;
// define question 3
questions[2][0] = "And where did the company relocate to in 1993";

questions[2][1] = "San Francisco";
questions[2][2] = "Chicago";
questions[2][3] = "Los Angeles";
questions[2][4] = "Boston";

answers[2] = "C";


questions[3] = new Array;
// define question 4
questions[3][0] = "How many artists have had signature guitars made for them by ESP";

questions[3][1] = "20";
questions[3][2] = "21";
questions[3][3] = "18";
questions[3][4] = "24";

answers[3] = "D";


questions[4] = new Array;
// define question 5
questions[4][0] = "How long have I been playing guitar for?";

questions[4][1] = "11 years";
questions[4][2] = "7 years";
questions[4][3] = "12 years";
questions[4][4] = "10 years";

answers[4] = "B";


questions[5] = new Array;
//define question 6
questions[5][0] = "How many notes are there on a guitar with a standard-scale neck";

questions[5][1] = "132";
questions[5][2] = "122";
questions[5][3] = "142";
questions[5][4] = "112";

answers[5] = "A";
//--> 
</script>
As you can see, the code is identical to that from the book but I have removed questionNumber from the answerCorrect function as I want to check each question on the page one at a time, rather than having questions appear one at a time at random.

Any help on this will be much appreciated.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off



All times are GMT -4. The time now is 06:38 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.