Quiz Program base on Cookie : Cookie : Development JAVASCRIPT DHTML TUTORIALS


JAVASCRIPT DHTML TUTORIALS » 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>

           
       



-

Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .

Follow Navioo On Twitter

JAVASCRIPT DHTML TUTORIALS

 Navioo Development
» Cookie