Skip to content

Instantly share code, notes, and snippets.

@sfasano
Created November 12, 2014 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sfasano/ab3b968c18f33d96a0b4 to your computer and use it in GitHub Desktop.
Save sfasano/ab3b968c18f33d96a0b4 to your computer and use it in GitHub Desktop.
My Guessing Game
<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