Quiz Program base on Cookie : Cookie « Development « JavaScript DHTML






Quiz Program base on Cookie

  

/*
Mastering JavaScript, Premium Edition
by James Jaworski 

ISBN:078212819X
Publisher Sybex CopyRight 2001
*/
<HTML>
<HEAD>
<TITLE>Quiz Program</TITLE>
<SCRIPT LANGUAGE="JavaScript"><!--
//Question object
function Question() {
 this.question=Question.arguments[0]
 var n=Question.arguments.length
 this.answers = new Array(n-2)
 for(var i=1; i<n-1; ++i)
  this.answers[i-1]=Question.arguments[i]
 this.correctAnswer=Question.arguments[n-1]
}
function readCookie() {
 currentQuestion=0
 numberOfQuestions=0
 correctAnswers=0
 score="None"
 cookie=document.cookie
 currentQuestion=getNumberValue(cookie,"currentQuestion")
 numberOfQuestions=getNumberValue(cookie,"numberOfQuestions")
 correctAnswers=getNumberValue(cookie,"correctAnswers")
 if(numberOfQuestions>0)
  score=Math.round(correctAnswers*100/numberOfQuestions)
}
function getNumberValue(s,n) {
 s=removeBlanks(s)
 var pairs=s.split(";")
 for(var i=0;i<pairs.length;++i) {
  var pairSplit=pairs[i].split("=")
  if(pairSplit[0]==n) {
   if(pairSplit.length>1) return parseInt(pairSplit[1])
   else return 0
  }
 }
 return 0
}
function removeBlanks(s) {
 var temp=""
 for(var i=0;i<s.length;++i) {
  var c=s.charAt(i)
  if(c!=" ") temp += c
 }
 return temp
}
function askNextQuestion() {
 document.writeln("<H4 ALIGN='CENTER'>"
  +qa[currentQuestion].question+"</H4>")
 displayAnswers()
}
function displayAnswers() {
 document.writeln('<FORM NAME="answerForm">')
 for(var ii=0;ii<qa[currentQuestion].answers.length;++ii) {
  document.writeln('<H4 ALIGN="CENTER">')
  document.writeln('<INPUT TYPE="RADIO" NAME="answer"> ')
  document.writeln(qa[currentQuestion].answers[ii])
  if(ii+1==qa[currentQuestion].answers.length) {
   document.writeln('<BR><BR><INPUT TYPE="BUTTON"')
   document.writeln('NAME="continue" VALUE="Continue" ')
   document.writeln(' onClick="checkAnswers()">')
  }
  document.writeln('</H4>')
 }
 document.writeln('</FORM>')
}
function checkAnswers() {
 var numAnswers=qa[currentQuestion].answers.length
 var correctAnswer=qa[currentQuestion].correctAnswer
 for(var jj=0;jj<numAnswers;++jj) {
  if(document.answerForm.elements[jj].checked) {
   if(jj==correctAnswer){
    correct()
     break
   }else{
     incorrect()
     break
   }
  }
  if(jj==numAnswers){
   incorrect()
   break
  }
 }
}
function correct() {
 ++currentQuestion
 ++numberOfQuestions
 ++correctAnswers
 updateCookie()
 location.reload(true)
}
function incorrect() {
 ++numberOfQuestions
 updateCookie()
 alert("Incorrect!")
 location.reload(true)
}
function updateCookie() {
 document.cookie="currentQuestion="+currentQuestion
 document.cookie="numberOfQuestions="+numberOfQuestions
 document.cookie="correctAnswers="+correctAnswers
}
function endQuiz() {
 document.cookie="currentQuestion=0"
 document.cookie="numberOfQuestions=0"
 document.cookie="correctAnswers=0"
 document.writeln('<FORM NAME="finishedForm">')
 document.write("<H4 ALIGN='CENTER'>")
 document.write("Congratulations! You have finished this quiz.")
 document.write('<BR><BR><INPUT TYPE="BUTTON" ')
 document.writeln('NAME="restart" VALUE="Restart" ')
 document.writeln(' onClick="restartQuiz()">')
 document.writeln("</H4>")
 document.writeln('</FORM>')
}
function restartQuiz() {
 location.reload(true)
}
// --></SCRIPT>
<SCRIPT LANGUAGE="JavaScript"><!--
//Heading displayed on the quiz page
pageHeading="History Quiz"
//Questions
qa = new Array()
qa[0] = new Question("Who was the first president of the United States?",
 "George Washington",
 "Abraham Lincoln",
 "Benjamin Franklin",
 "Harry Truman",
 0)
qa[1] = new Question("When did Columbus discover America?",
 "1249",
 "1942",
 "1492",
 "1294",
 2)
qa[2] = new Question("Who commanded the Macedonian army?",
 "Napoleon",
 "Alexander the Great",
 "Cleopatra",
 "George Patton",
 1)
qa[3] = new Question("Where did Davy Crockett lose his life?",
 "The Spanish Inquisition",
 "The Alamo",
 "Miami, Florida",
 "On the Oregon Trail",
 1)
qa[4] = new Question("Who was the first man to walk on the moon?",
 "Louis Armstrong",
 "Buzz Armstrong",
 "Jack Armstrong",
 "Neil Armstrong",
 3)
qa[5] = new Question("Who wrote the <I>Scarlet Letter</I>?",
 "Michael Crichton",
 "Ernest Hemingway",
 "Nathaniel Hawthorne",
 "Charles Dickens",
 2)
qa[6] = new Question("Eli Whitney invented:",
 "Mad Cow's Disease",
 "the Cotton Gin",
 "whisky",
 "the automobile",
 1)
qa[7] = new Question("Who was known as the King of the Fauves?",
 "Salvatore Dali",
 "Henri Matisse",
 "Pablo Picasso",
 "Vincent Van Gogh",
 1)
qa[8] = new Question("Who discovered the force of gravity?",
 "Isaac Newton",
 "Galileo",
 "Copernicus",
 "Albert Einstein"
 ,0)
qa[9] = new Question("Who created HTML?",
 "Tim Berners-Lee",
 "Marc Andreessen",
 "Bill Gates",
 "Jim Barksdale",
 0)
qa[10] = new Question("Leonardo da Vinci was born in Greece.",
 "True",
 "False",
 1)
qa[11] = new Question("Louisiana was purchased from France.",
 "True",
 "False",
 0)

// --></SCRIPT>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript"><!--
readCookie()
document.writeln("<H1 ALIGN='CENTER'>"+pageHeading+"</H1>")
document.writeln("<P ALIGN='RIGHT'><B>Questions: "
 +numberOfQuestions+"<BR>")
document.writeln("Correct Answers: "+correctAnswers+"<BR>")
document.writeln("Score: "+score+"</B></P>")
if(currentQuestion >= qa.length) endQuiz()
else askNextQuestion()
// --></SCRIPT>
</BODY>
</HTML>

           
         
    
  








Related examples in the same category

1.Reads, writes and deletes current Web page's cookies
2.'cookieEnabled' Example
3.Create a cookie
4.Set the cookie expire date
5.Secure cookie
6.Read all cookies
7.Standard cookie functions: extract Cookie Value
8.Save name to cookie
9.Cookie set, delete, get value and create
10.Cookie utility function
11.Cookie install and delete (remove)
12.Cookie: retrieve a future expiration date in proper format
13.A Cookie Example
14.A Cookie Test Program
15. A Website Access Counter
16. Keeping Track of User Access Time
17.Bill Dortch's Cookie Functions
18.Cookie Preferences
19.Set cookie to document and read it back