basic-javascript-09-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"> // chapter 9 addition to allow timed quizes var timeLeft =-1; var totalQuestionsToAsk = 0; var quizTimerId = 0; // 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"; // define question 4 questions[3] = "In the Simpsons, Bleeding Gums Murphy played which instrument?" // assign answer for question 4 answers[3] = "\\bsax(ophone)?\\b"; // define question 5 questions[4] = "Which American President was involved in the Watergate Scandal?" // assign answer for question 5 answers[4] = "\\b((Richard |R\\.? ?)(Milhous |M\\.? )?)?Nixon\\b"; function updateTimeLeft() { timeLeft--; if (timeLeft == 0) { alert("Time's Up"); numberOfQuestionsAsked = totalQuestionsToAsk; window.top.fraQuizPage.location.href = "AskQuestion.htm"; } else { var minutes = Math.floor(timeLeft / 60); var seconds = timeLeft - (60 * minutes); if (minutes < 10) minutes = "0" + minutes; if (seconds < 10) seconds = "0" + seconds; window.status = "Time left is " + minutes + ":" + seconds; } } function resetQuiz(numberOfQuestions, SelectedTimeLimit) { timeLeft = SelectedTimeLimit; totalQuestionsToAsk = numberOfQuestions; var indexCounter; currentQNumber = -1; questionsAsked = new Array(); for (indexCounter = 0; indexCounter < questions.length;indexCounter++) { questionsAsked[indexCounter] = false; } numberOfQuestionsAsked = 0; numberOfQuestionsCorrect = 0; if (timeLeft == -1) { window.status = "No Time Limit"; } else { quizTimerId = window.setInterval("updateTimeLeft()",1000); } } 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 var answerRegExp = new RegExp(answers[questionNumber],"i"); if (answer.search(answerRegExp) != -1) { numberOfQuestionsCorrect++; correct = true; } // return whether the answer was correct (true or false) return correct; } function getQuestion() { if (totalQuestionsToAsk != 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>"; // Check if array or string if (typeof questions[questionNumber] == "string") { questionHTML = questionHTML + "<p>" + questions[questionNumber] + "</p>"; questionHTML = questionHTML + "<p><input type=text name=txtAnswer "; questionHTML = questionHTML + " maxlength=100 size=35></p>" questionHTML = questionHTML + '<script type="text/javascript">' + 'document.QuestionForm.txtAnswer.value = "";<\/script>'; } else { 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 { if (timeLeft != -1) { clearInterval(quizTimerId); } 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 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.