Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!doctype html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>Sample Javascript Guessing Game</title>
- <script src="guessgame.js"></script>
- </head>
- <body>
- <h1> Guess the Number! </h1>
- <fieldset>
- <legend>Inputs</legend>
- <label for="guess">Your Guess:</label>
- <input type="text" id="guess" value="523" />
- <input type="button" onclick="yourGuess()" value="submit" /><br />
- <input type="button" id="showguesses" onclick="showGuesses()" value="Show My Guesses" />
- <input type="button" onclick="generateNumberToGuess(true)" value="New Game" /><br />
- <input id="cheat" type="checkbox" value="cheat" onclick="showNumberToGuess()" />
- <label for="cheat">Cheat</label>
- <div id="cheatShow" style="display: none;">
- <input id="numberToGuess" type="text" />
- <label for="numberToGuess">Number to guess</label>
- </div>
- </fieldset>
- <fieldset id="guesses" class="guesses">
- <legend> Output </legend>
- <textarea id="output" name="output" rows="5" style="width: 100%;"></textarea>
- </fieldset>
- </body>
- </html>
- // JAVA SCRIPT //
- // guessgame.js //
- var elOutput = document.getElementById('output');
- function generateNumberToGuess() {
- numberToGuess = Math.floor(Math.random() * 1000) + 1;
- }
- function showNumberToGuess() {
- }
- function showGuesses() {
- elOutput = document.getElementById('output');
- elOutput.innerHTML += ''
- }
- function yourGuess() {
- var elGuess = document.getElementById('guess');
- if (elGuess.value === numberToGuess) {
- elOutput.innerHTML += '<p>You guess the number correctly! Which was: ' + numberToGuess + '<br /></p>'
- }
- if (elGuess.value < numberToGuess) {
- elOutput.innerHTML += '<p>You guess incorrectly. The number is more. <br /></p>'
- }
- if (elGuess.value > numberToGuess) {
- elOutput.innerHTML += '<p>You guess incorrectly. The number is less. <br /></p>'
- }
- // You can store references to the value and the
- // guesses textarea in local function variables.
- }
- window.onload = function(){
- generateNumberToGuess();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement