Skip to content

Instantly share code, notes, and snippets.

@stephanielingwood
Last active August 29, 2015 14:02
Show Gist options
  • Save stephanielingwood/f7f811209b2c25d3f8b6 to your computer and use it in GitHub Desktop.
Save stephanielingwood/f7f811209b2c25d3f8b6 to your computer and use it in GitHub Desktop.
Stephanie Lingwood's Assignment 2
<script>
keepPlaying = true
alert("Welcome to the Number-Guessing game!");
do {
var randomNumber;
var tries;
alert("You get three chances to guess a number. Ready?");
var game = function(answer) {
var guess = prompt("I'm thinking of a number between 1 and 10. What is it?");
var difference = Math.abs(parseInt(guess) - answer);
if (difference == 0) {
alert("Wow! You got it!");
}
else if (difference < 2) {
alert("So close! Only off by 1.");
}
else if (difference < 4) {
alert("Getting closer!");
}
else if ( difference >= 4) {
alert("Sorry...not even close.");
}
}
randomNumber = Math.ceil(Math.random() * 10);
for (tries = 1; tries < 4; tries++) {
game(randomNumber);
}
okResponse = true
do {
var playMore = prompt("Thanks for playing! Play again? Type yes or no.");
if (playMore === "yes") {
alert("Okay, let's play again!");
okResponse = false
}
else {
alert("Have a great day, number-guesser!");
okResponse = false
keepPlaying = false
}
} while(okResponse);
} while(keepPlaying);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment