Created
November 12, 2014 07:21
-
-
Save sfasano/ab3b968c18f33d96a0b4 to your computer and use it in GitHub Desktop.
My Guessing Game
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript"> | |
// Declare variables | |
var userGuess, userChoice, answer; | |
// Present user with two options | |
do { | |
userChoice = prompt('Would you like to choose your own number, or have the computer randomly choose for you? \ | |
1: Choose \ | |
2: Random'); | |
} while (userChoice != '1' && userChoice != '2'); | |
// Get user's number based on choice | |
if (userChoice === '1') { | |
userGuess = prompt('Please choose a number between 1 and 10:'); | |
} else { | |
userGuess = Math.floor(Math.random() * 10 + 1); | |
prompt("Your guess is " + userGuess + ". Press ENTER to continue."); | |
} | |
// Generate answer | |
answer = Math.floor(Math.random() * 10 + 1); | |
// Compare and Output | |
if (userGuess === answer) { | |
alert("Congrats! You were right! The answer was " + answer); | |
} else { | |
alert("Sorry! The answer was " + answer); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment