Javascript quiz

1,470 views

Published on

Quiz created using cookies in Javascript and HTML.
Has negative marking for every wrong answer....

Benefit and make maximum use of it...
"LIKE " it if u found it very useful....it really helps to boost an uploaders morale.

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,470
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
27
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Javascript quiz

  1. 1. JAVASCRIPT Quiz by Kishan Phadte<HTML><HEAD><TITLE>Quiz Program</TITLE><SCRIPT LANGUAGE="JavaScript">//Question objectfunction Question() {this.question=Question.arguments[0]var n=Question.arguments.lengththis.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() {
  2. 2. currentQuestion=0numberOfQuestions=0correctAnswers=0score="None"cookie=document.cookiecurrentQuestion=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=""
  3. 3. 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=LEFT>" +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="LEFT">) 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>)
  4. 4. }function checkAnswers() {var numAnswers=qa[currentQuestion].answers.lengthvar correctAnswer=qa[currentQuestion].correctAnswerfor(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++correctAnswersupdateCookie()
  5. 5. location.reload(true)}function incorrect() {++numberOfQuestionsupdateCookie()alert("Incorrect!")location.reload(true)}function updateCookie() {document.cookie="currentQuestion="+currentQuestiondocument.cookie="numberOfQuestions="+numberOfQuestionsdocument.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>)document.write("<img src=so.jpg width=200 height=200 align=center")document.write(<BR><BR><INPUT TYPE="BUTTON" )document.writeln(NAME="restart" VALUE="Restart" )document.writeln( onClick="restartQuiz()">)
  6. 6. document.writeln("</H4>")document.writeln(</FORM>)}function restartQuiz(){location.reload(true);}</SCRIPT><SCRIPT LANGUAGE="JavaScript">pageHeading="Corrupt Governance Quiz"//Questionsvar qa = new Array()qa[0] = new Question("1) Who is the lead actor in the Lokpal Bill ?","Anna Hazare","John Abraham","Hrithik Roshan","Akshay Kumar",0)qa[1] = new Question("2) Who out of these set INDIAS WINGS ON FIRE ?","Dragon","Gabbar Singh","Sonia Gandhi","Fireman",2)
  7. 7. qa[2] = new Question("3) After how many days did ANNA Hazare break his hunger strike?","365 days","13 days","12 years","1 day",1)qa[3] = new Question("4) Who said that he is ANNAs <i>HANUMAN</i>?","KApil Sibbal","Arvind Kejriwaal","Sanjay Khan","Sanjay Dutt",1)qa[4] = new Question("5) Who is Father of our nation?","Laloo Prasad Yadav","Amar Singh","Sardar Manmohan Singh","M.K. Gandhi",3)qa[5] = new Question("6) Who believes in <I>CORRUPTION</I>?","Eminem","Bob Marley","Sonia Chor Gandhi","AKON",2)qa[6] = new Question("7) Anna Hazare is ",
  8. 8. "Labour (Gawandi)","Ex- Armyman","Gangster","Corrupt Minister",1)qa[7] = new Question("8) Who is SMS?","Salvatore Dali","Sardar Manmohan Singh","Rahul R. Gandhi","Narendra Modi",1)qa[8] = new Question("9) Who is Kiran Bedi?","IAS officer","Bus conductor","Animal activist","Scientist",0)qa[9] = new Question("10) Who is <b>Arvind Kejriwal</b>?","Engineer from IIT Kharagpur","Surgeon","Astronomer","Fasting Specialist",0)</SCRIPT>
  9. 9. </HEAD><BODY><SCRIPT LANGUAGE="JavaScript"><!--readCookie()document.writeln("<H1 ALIGN=LEFT>"+"<img src=corruption.jpg width=980 height=120/>"+"<br>" +pageHeading+"</H1>"+"<hr>")document.writeln("<P ALIGN=RIGHT><B>Questions: "+numberOfQuestions+"<BR>")document.writeln("Correct Answers: "+correctAnswers+"<BR>")document.writeln("Score: "+score+"</B>")document.writeln("<img ALIGN=RIGHT src=co.jpg width=150 height=300/>")if(currentQuestion >= qa.length) endQuiz()else askNextQuestion()// --></SCRIPT></BODY></HTML>
  10. 10. Result :

×