Skip to content

Instantly share code, notes, and snippets.

@sanakm
Created May 2, 2016 22:49
Show Gist options
  • Save sanakm/2de7e699bafb70f42bafc278ceb18c09 to your computer and use it in GitHub Desktop.
Save sanakm/2de7e699bafb70f42bafc278ceb18c09 to your computer and use it in GitHub Desktop.
var game = {
userAmount: 100,
bet: null,
number: null,
guess: null,
getBetAmount: function() {
this.bet = parseInt(prompt("Please enter a bet:"),10);
if (this.bet > this.userAmount) {
alert("Bet amount cannot be larger than what's in your wallet!");
}
else{
this.userAmount -= this.bet;
this.getGuess();
}
},
getGuess: function() {
this.generateRand();
this.guess = parseInt(prompt("Please enter a number:"),10);
console.log(this.guess);
this.checkGuess();
},
generateRand: function() {
this.number = Math.floor(Math.random() * 10);
console.log(this.number);
},
checkGuess: function() {
if (this.guess === this.number) {
this.userAmount += (this.bet * 2)
console.log("Yay!")
alert("You were correct, you have " + this.userAmount + " in your wallet now");
}
else if ((this.guess + 1 || this.guess - 1) === this.number) {
this.userAmount += this.bet
console.log("Not bad!")
alert("You were close. Here's your bet back.");
}
else {
console.log("Boo!");
alert("WRONG AGAIN. You have " + this.userAmount + " in your wallet now");
}
}
};
while (game.userAmount > 0) {
game.getBetAmount();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment