Skip to content

Instantly share code, notes, and snippets.

@stephanielingwood
Created June 13, 2014 03:55
Show Gist options
  • Save stephanielingwood/f2082f11afb714844e45 to your computer and use it in GitHub Desktop.
Save stephanielingwood/f2082f11afb714844e45 to your computer and use it in GitHub Desktop.
Assignment 3
<script>
var debug = false
alert("Welcome to the Number-Guessing game! This is a two-person game. You can play against yourself, but it's more fun if you grab a friend to join you!");
function User(name, guess, howClose) {
this.name = name;
this.guess = guess;
this.howClose = howClose;
this.askName = function() {
this.name = prompt("Please type your name:");
}
this.askGuess = function() {
this.guess = prompt(this.name + " please type your guess:");
}
this.calculate = function() {
this.howClose = Math.abs(parseInt(this.guess) - answer);
}
}
keepPlaying = true
do {
var answer = Math.ceil(Math.random() * 10);
user1 = new User();
user2 = new User();
alert("Let's find out who the first player is.");
user1.askName();
if (debug = false) {
console.log(user1.name);
}
alert("Okay, let's find out who the second player is.");
user2.askName();
if (debug = false) {
console.log(user2.name);
}
alert("Great! Let's get ready to play. I'll choose a number between 1 and 10. Each of you will get to guess what it is. After you've both guessed, I'll tell you who was closer. First one to guess the number wins! Ready?");
while (user1.guess != answer && user2.guess != answer) {
user1.askGuess();
user2.askGuess();
user1.calculate();
user2.calculate();
if (user1.guess == answer || user2.guess == answer) {
if (user1.guess == answer && user2.guess == answer) {
alert("You tied!");
}
else if (user1.guess == answer) {
alert(user1.name + " won!");
}
else if (user2.guess == answer) {
alert(user2.name + " won!");
}
else {
}
}
else if ( user1.howClose < user2.howClose ) {
alert(user1.name + " is closer.");
}
else {
alert(user2.name + " is closer.");
}
}
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 if (playMore === "no") {
alert("Have a great day, number-guessers!");
okResponse = false
keepPlaying = false
}
else {
alert("Looks like you didn't type yes or no.")
}
} while(okResponse);
} while(keepPlaying);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment