Skip to content

Instantly share code, notes, and snippets.

@relaxedtomato
Forked from sanakm/betting_game
Last active May 3, 2016 01:16
Show Gist options
  • Save relaxedtomato/3b03519168e09c3a4e0be516590eca51 to your computer and use it in GitHub Desktop.
Save relaxedtomato/3b03519168e09c3a4e0be516590eca51 to your computer and use it in GitHub Desktop.
var game = {
userAmount: 100,
bet: null,
number: null,
guess: null,
getBetAmount: function() {
//RB: Question - what does 'this' refer to? Do look up the different rules for 'this' (we can discuss further next time).
//RB: The approach to using this is not wrong, however, you can also just refer to globally defined variables (to reduce writing 'this')
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;
//RB: I do not recommend calling methods from other methods, I made a note below about this as well.
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);
}, //RB: Keep the spacing consistent between object properties (usually a linter will help with this)
checkGuess: function() {
if (this.guess === this.number) {
this.userAmount += (this.bet * 2)
console.log("Yay!")
//RB: Let's look at another way of writing the below line (in ES6)
//RB: `You were correct, you have ${this.userAmount} in your wallet now` - (this should work in most browsers)
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(); //RB: Formatting
//Instead of calling methods from other methods (or functions from other functions) you could of placed them within the while loop
//RB: How about if the user wants to end the game, they cannot based on the logic (they have to loose their money before ending the game)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment