
|
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 really simple project in which the users needs to calculate the answer of a equation and then needs to enter that into a textfield and click the Check Answer button. Interface The interface is simple and consists out of few elements:
Programming Let's first start off with the interface: Code Snippet (Syntax highlighted) - Toggle Wrap <form action="" method='post' onsubmit="return false"> <span id='Number1'></span> + <span id='Number2'></span> = <input size='3' maxlength='3' type='text' id='Player_Answer' /> <input type='button' onclick='CheckAnswer()' value='Check answer' /> </form> First we have to create two global variables that keep count of the wins and losses of the user: And now the function that calculates a random number between 0 and 100: Code Snippet (Syntax highlighted) - Toggle Wrap function CreateRandomNumber() { /* Calculating and returning random number between 0 and 100 */ return Math.floor(Math.random() * (100 + 1)); } Then the function that creates a new question by retrieving two random numbers and loading them into the elements: Code Snippet (Syntax highlighted) - Toggle Wrap function CreateNewQuestion() { /* Retrieving new numbers: */ var New_Number1 = CreateRandomNumber(); var New_Number2 = CreateRandomNumber(); /* Loading the new numbers into the elements: */ document.getElementById('Number1').innerHTML = New_Number1; document.getElementById('Number2').innerHTML = New_Number2; } And now the main function which combines all functions together: Code Snippet (Syntax highlighted) - Toggle Wrap function CheckAnswer() { /* Retrieving the two numbers and the player's answer: */ var Number1 = parseInt(document.getElementById('Number1').innerHTML); var Number2 = parseInt(document.getElementById('Number2').innerHTML); var Player_Answer = parseInt(document.getElementById('Player_Answer').value); if (Player_Answer == (Number1 + Number2)) { /* If the player's answer is correct: */ /* Increasing wins: */ PlayerWins++; /* Showing status: */ alert('Correct answer!\nWins:' + PlayerWins + '\nLosses:' + PlayerLosses); /* Creating new question: */ CreateNewQuestion(); } else if (Player_Answer != (Number1 + Number2)) { /* If the player's answer is incorrect: */ /* Increasing wins: */ PlayerLosses++; /* Showing status: */ alert('Wrong answer!\nWins:' + PlayerWins + '\nLosses:' + PlayerLosses); /* Creating new question: */ CreateNewQuestion(); } } Also, you can think about expanding this code by adding more difficult equations (- / % * ^ etc...) This code in action You can download the code using the link below: http://www.webdevproject.com/zips/MathQuiz.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... |