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

JavaScript DHTML
1. Ajax Layer
2. Data Type
3. Date Time
4. Development
5. Document
6. Event
7. Event onMethod
8. Form Control
9. GUI Components
10. HTML
11. Javascript Collections
12. Javascript Objects
13. Language Basics
14. Node Operation
15. Object Oriented
16. Page Components
17. Security
18. Style Layout
19. Table
20. Utilities
21. Window Browser
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
JavaScript DHTML » Development » Cookie 
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>1return 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('<HALIGN="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[0new Question("Who was the first president of the United States?",
 "George Washington",
 "Abraham Lincoln",
 "Benjamin Franklin",
 "Harry Truman",
 0)
qa[1new Question("When did Columbus discover America?",
 "1249",
 "1942",
 "1492",
 "1294",
 2)
qa[2new Question("Who commanded the Macedonian army?",
 "Napoleon",
 "Alexander the Great",
 "Cleopatra",
 "George Patton",
 1)
qa[3new Question("Where did Davy Crockett lose his life?",
 "The Spanish Inquisition",
 "The Alamo",
 "Miami, Florida",
 "On the Oregon Trail",
 1)
qa[4new Question("Who was the first man to walk on the moon?",
 "Louis Armstrong",
 "Buzz Armstrong",
 "Jack Armstrong",
 "Neil Armstrong",
 3)
qa[5new Question("Who wrote the <I>Scarlet Letter</I>?",
 "Michael Crichton",
 "Ernest Hemingway",
 "Nathaniel Hawthorne",
 "Charles Dickens",
 2)
qa[6new Question("Eli Whitney invented:",
 "Mad Cow's Disease",
 "the Cotton Gin",
 "whisky",
 "the automobile",
 1)
qa[7new Question("Who was known as the King of the Fauves?",
 "Salvatore Dali",
 "Henri Matisse",
 "Pablo Picasso",
 "Vincent Van Gogh",
 1)
qa[8new Question("Who discovered the force of gravity?",
 "Isaac Newton",
 "Galileo",
 "Copernicus",
 "Albert Einstein"
 ,0)
qa[9new Question("Who created HTML?",
 "Tim Berners-Lee",
 "Marc Andreessen",
 "Bill Gates",
 "Jim Barksdale",
 0)
qa[10new Question("Leonardo da Vinci was born in Greece.",
 "True",
 "False",
 1)
qa[11new 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.lengthendQuiz()
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. Standard cookie functions: extract Cookie Value
5. Save name to cookie
6. Cookie set, delete, get value and create
7. Cookie utility function
8. Cookie install and delete (remove)
9. Cookie: retrieve a future expiration date in proper format
10. A Cookie Example
11. A Cookie Test Program
12.  A Website Access Counter
13.  Keeping Track of User Access Time
14. Bill Dortch's Cookie Functions
15. Cookie Preferences
w__w_w.__j_a_v___a___2s.___c_om__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.