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.