topical media & game development

talk show tell print

basic-javascript-07-quiz-GlobalFunctions.htm / htm



  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
  <html>
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <title>Global functions</title>
  <script language="JavaScript"  type="text/javascript">
  // questions and answers variables will holds questions and answers
  var questions = new Array();
  var answers = new Array(); 
  var questionsAsked;
  var numberOfQuestionsAsked = 0;
  var numberOfQuestionsCorrect = 0;
  var currentQNumber = -1;
  // define question 1 
  questions[0] = new Array();
  questions[0][0] = "The Beatles were";
  questions[0][1] = "A sixties rock group from Liverpool";
  questions[0][2] = "Four musically gifted insects";
  questions[0][3] = "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";
  function resetQuiz()
  {
  var indexCounter;
  currentQNumber = -1;
  questionsAsked = new Array();
  for (indexCounter = 0; indexCounter < questions.length;indexCounter++)
     {
  questionsAsked[indexCounter] = false;
     }
  numberOfQuestionsAsked = 0;
  numberOfQuestionsCorrect = 0;
  }
  function answerCorrect(questionNumber, answer)
  {
  // declare a variable to hold return value
  var correct = false;
  // if answer provided is same as answer then correct answer is true
  if (answer == answers[questionNumber])
     {
  numberOfQuestionsCorrect++;
  correct = true;
     }
  // return whether the answer was correct (true or false)
  return correct;
  }
  function getQuestion()
  {
  if (questions.length != numberOfQuestionsAsked)
     {
  var questionNumber = Math.floor(Math.random() * questions.length)
  while (questionsAsked[questionNumber] == true)
        {
  questionNumber = Math.floor(Math.random() * questions.length);
        }
  var questionLength = questions[questionNumber].length;
  var questionChoice;
  numberOfQuestionsAsked++;
  var questionHTML = "<h4>Question " + numberOfQuestionsAsked +  "</h4>";
  questionHTML = questionHTML + "<p>" + questions[questionNumber][0];
  questionHTML = questionHTML + "</p>";
  for (questionChoice = 1;questionChoice < questionLength;questionChoice++)
        {
  questionHTML = questionHTML + "<input type=radio " 
  questionHTML = questionHTML + "name=radQuestionChoice"
  if (questionChoice == 1)
           {
  questionHTML = questionHTML + " checked";
           }
  questionHTML = questionHTML + ">" +
  questions[questionNumber][questionChoice];
  questionHTML = questionHTML + "<br>"
        }
  questionHTML = questionHTML + "<br><input type='button' "
  questionHTML = questionHTML + " value='Answer Question'";
  questionHTML = questionHTML + "name=buttonNextQ "; 
  questionHTML = questionHTML + "onclick='return buttonCheckQ_onclick()'>";
  currentQNumber = questionNumber;
  questionsAsked[questionNumber] = true;
     }
  else
     {
  var questionHTML = "<h3>Quiz Complete</h3>";
  questionHTML = questionHTML + "You got " + numberOfQuestionsCorrect;
  questionHTML = questionHTML + " questions correct out of "  
  questionHTML = questionHTML + numberOfQuestionsAsked;
  questionHTML = questionHTML + "<br><br>Your trivia rating is "
  switch(Math.round(((numberOfQuestionsCorrect / numberOfQuestionsAsked) * 10)))
        {
  case 0:
  case 1:
  case 2:
  case 3:
  questionHTML = questionHTML + "Beyond embarrasing";
  break;
  case 4:
  case 5:
  case 6:
  case 7:
  questionHTML = questionHTML + "Average";
  break;
  default:
  questionHTML = questionHTML + "Excellent"
        }
  questionHTML = questionHTML + "<br><br><A "
  questionHTML = questionHTML + "href='quizpage.htm'><strong>"
  questionHTML = questionHTML + "Start again</strong></A>"
     }
  return questionHTML;
  }
  </script>
  </head>
  <body>
  </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.