topical media & game development

talk show tell print

basic-javascript-06-Quiz-trivia-quiz.htm / htm



  <html>
  <head>
  <title>Wrox Online Trivia Quiz</title>
  <script language="JavaScript" type="text/javascript">
  
  var questionNumber;
  
  function answerCorrect(questionNumber, 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[questionNumber])
        correct = true;
     
     // return whether the answer was correct (true or false)
     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";
        }
        questionHTML = questionHTML + ">";
        questionHTML = questionHTML + questions[questionNumber][questionChoice];
        questionHTML = questionHTML + "<br>";
     }
     document.QuestionForm.txtQNumber.value = questionNumber + 1;
     return questionHTML;
  }
  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("You got it wrong");
     }
     window.location.reload();
  }
  
  // questions and answers arrays will holds questions and answers
  var questions = new Array();
  var answers = new Array();
  
  // define question 1 
  questions[0] = new Array();
  
  // the question
  questions[0][0] = "The Beatles were";
  
  // first choice
  questions[0][1] = "A sixties rock group from Liverpool";
  
  // second choice
  questions[0][2] = "Four musically gifted insects";
  
  // third choice
  questions[0][3] = "German cars";
  
  // fourth choice
  questions[0][4] = "I don't know. Can I have the questions on Baseball please?";
  
  // assign answer for question 1
  answers[0] = "A";
  
  // define 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";
  
  // assign answer for question 2
  answers[1] = "B";
  
  // define 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"; 
  
  // assign answer for question 3 
  answers[2] = "C";
  
  </script>
  </head>
  <body>
  
  <form name="QuestionForm">
  Question
  <input type="text" name=txtQNumber size=1>
  <script language="JavaScript" type="text/javascript">
     document.write(getQuestion());
  </script>
  <input type="button" value="Check Question" name="buttonCheckQ"
     onclick="return buttonCheckQ_onclick()">
  </form>
  
  </body>
  </html>
  
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.