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. Dojo toolkit
7. Event
8. Event onMethod
9. Ext JS
10. Form Control
11. GUI Components
12. HTML
13. Javascript Collections
14. Javascript Objects
15. Javascript Properties
16. jQuery
17. Language Basics
18. Mochkit
19. Mootools
20. Node Operation
21. Object Oriented
22. Page Components
23. Rico
24. Scriptaculous
25. Security
26. SmartClient
27. Style Layout
28. Table
29. Utilities
30. Window Browser
31. YUI Library
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 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. 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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.