
|
49 Projects
|
||||||||||
|
AsteroidsBlackjackBrowsergameChatCoinflipCryptFirst Person ShooterForumHangmanLightboxLogin SystemMath QuizMemoryObject Mouse EvadorPapiJumpPokerQuizRich Text Editor WYSIWYGSnakeSystem ExplorerThe Impossible QuizTic Tac Toe CalculatorCalendarClockColor PickerCustom MessageboxDictionaryImage EditorImage GeneratorImage SlideshowLink ActivationMessage SystemNewsletterNumber ConverterOnline KeyboardPaginationPoker Chance CalculatorProgress BarReaction TimerRSS FeedSearch EngineStarRatingStopwatchSyntax HighlighterText ReplacerURL ShortenerUser Management SystemVisitor Counter
|
||||||||||
Introduction This is a pretty simple quiz that has two types of answers: open and multiple choice. The questions range from logaritmes to nature. Interface The interface consists out of the following elements:
Programming Let's first start off with making the interface and its style sheet: Code Snippet (Syntax highlighted) - Toggle Wrap <form method="post" action="" onsubmit='return false;' id='Quiz_Main'> <div id='Quiz_Question'>What follows the following sequence: 0 1 1 2 3 5 8 13 ... </div> <div id='Quiz_Answer'></div> <div id='Quiz_Button'> <input style='width:110px;' type='button' value='Check answer!' onclick='CheckAnswer()' /> </div> <div id='Quiz_Status'> Correct answers: <span id='Quiz_Correct'>0</span><br /> Incorrect answers: <span id='Quiz_Incorrect'>0</span><br /> Correct percentage: <span id='Quiz_Percentage'>100</span>% </div> </form> And its CSS: Code Snippet (Syntax highlighted) - Toggle Wrap <style type='text/css'> #Quiz_Main { position:relative; top:0px; left:0px; width:300px; height:200px; background-color:rgb(247,247,247); border:1px solid #BBB; font-family:Arial; font-size:11px; } #Quiz_Question { position:absolute; top:5px; left:5px; width:290px; } #Quiz_Answer { position:absolute; top:36px; left:5px; width:282px; height:94px; border:1px solid #BBB; background-color:rgb(252,252,252); padding:3px; } #Quiz_Button { position:absolute; top:155px; left:20px; } #Quiz_Status { position:absolute; top:145px; left:150px; } </style> Now we start the JavaScript programming by creating 3 global variables: Code Snippet (Syntax highlighted) - Toggle Wrap var Current_Question; var Correct_Answers = 0; var Incorrect_Answers = 0; Now we start off with coding the quiz, beginning with the function RandomQuestion() that randomly picks a question from pre-defined arrays and returns it all packed into an array: Code Snippet (Syntax highlighted) - Toggle Wrap function RandomQuestion() { /************************************* ***** MULTIPLE CHOICE QUESTIONS *****/ /* Multiple choice questions: */ var MC_Questions = new Array( "What follows the following sequence: 0 1 1 2 3 5 8 13 ... ?", "Who wrote the De Bello Gallico?"); /* Multiple choice possible answers to the questions: */ var MC_Answers = new Array; MC_Answers[0] = new Array("2", "23", "21"); MC_Answers[1] = new Array("Ceasar", "Cicero", "Claudius"); /* Multiple choice correct answer to the question: */ var MC_Cor_Answer = new Array("21","Ceasar"); /************************************* ********** OPEN QUESTIONS **********/ /* Open questions: */ var OP_Questions = new Array("What follows the following sequence: 0 1 2 4 6 9 12 16 20 ... ?", "Who distributed Age of Empires III?"); /* Correct answer to the open questions: */ var OP_Cor_Answer = new Array("25","microsoft"); /************************************* ***** CREATING RANDOM QUESTION *****/ /* Creating return array: */ var RetAr = new Array; /* Creating question type: */ if (Math.floor(Math.random() * (1 + 1)) == 0) { RetAr['type'] = "MC"; // Multiple Choice var Question_Amount = MC_Questions.length; var Question_Number = Math.floor(Math.random() * (Question_Amount)); RetAr['question'] = MC_Questions[Question_Number]; RetAr['answer'] = MC_Cor_Answer[Question_Number]; RetAr['answers'] = MC_Answers[Question_Number]; } else { RetAr['type'] = "OP"; // Open var Question_Amount = OP_Questions.length; var Question_Number = Math.floor(Math.random() * (Question_Amount)); RetAr['question'] = OP_Questions[Question_Number]; RetAr['answer'] = OP_Cor_Answer[Question_Number]; } return RetAr; } Then we make the function that retrieves a random question using CreateRandomQuestion() and then loads the appropiate HTML into the interface: Code Snippet (Syntax highlighted) - Toggle Wrap function CreateNewQuestion() { /* Retrieving question array: */ var QA = RandomQuestion(); /* This contains the HTML of the Answer box */ var Answer_HTML = ""; /* Creating the answer possibility depending on the question type: */ if (QA['type'] == "OP") { /* Creating open question answer: */ Answer_HTML = "<div style='position:absolute; top:40px; left:60px;'><input style='width:160px; border:1px solid #BBB;' type='text' value='' id='Quiz_Answer_Txtinp' /></div>"; } else { /* Creating multiple choice answer: */ Answer_HTML += "<div style='position:absolute; top:20px; left:10px;'>"; Answer_HTML += " <input type='radio' name='Quiz_Chk' value='0' />" + QA['answers'][0] + "<br />"; Answer_HTML += " <input type='radio' name='Quiz_Chk' value='1' />" + QA['answers'][1] + "<br />"; Answer_HTML += " <input type='radio' name='Quiz_Chk' value='2' />" + QA['answers'][2] + ""; Answer_HTML += "</div>"; } /* Setting the question into a global variable: */ Current_Question = QA; /* Loading the question and the answer: */ document.getElementById('Quiz_Question').innerHTML = QA['question']; document.getElementById('Quiz_Answer').innerHTML = Answer_HTML; } Now we make the function that updates the status within the interface called UpdateStatus(): Code Snippet (Syntax highlighted) - Toggle Wrap function UpdateStatus() { /* Updating elements: */ document.getElementById('Quiz_Correct').innerHTML = Correct_Answers; document.getElementById('Quiz_Incorrect').innerHTML = Incorrect_Answers; /* Calculating win percentage: */ var Percentage = 0; /* Making sure that there is no division by zero: */ if ((Correct_Answers + Incorrect_Answers) == 0) { /* Starting percentage: */ Percentage = 100; } else { /* Calculating percentage (formula: part / full * 100% */ Percentage = Math.floor( ( Correct_Answers / ( ( Correct_Answers + Incorrect_Answers) ) * 100 ) ); } /* Loading the percentage into the element: */ document.getElementById('Quiz_Percentage').innerHTML = Percentage; } And then we automatically start the quiz when the page is loaded: Code Snippet (Syntax highlighted) - Toggle Wrap /* When the window is loaded, start the quiz: */ window.onload=function(){ CreateNewQuestion(); } And that was about it! This code in action You can download the full source code of this project from the link below: http://www.webdevproject.com/zips/Quiz.zip Comments
New project: Math Quiz
2010-11-09 18:07:48 Read more... New project: Custom Messagebox 2010-11-06 19:10:50 Read more... New project: Visitor Counter 2010-11-01 21:39:04 Read more... New project: Image Editor 2010-11-01 21:37:27 Read more... New project: Image Generator 2010-11-01 21:33:05 Read more... |