Skip to content

Instantly share code, notes, and snippets.

@josephhays
Last active August 29, 2015 14:22
Show Gist options
  • Save josephhays/54d719d904268de21a84 to your computer and use it in GitHub Desktop.
Save josephhays/54d719d904268de21a84 to your computer and use it in GitHub Desktop.
Javascript Betting Game Assignment for LighthouseLabs

Javascript Betting Game Assignment for LighthouseLabs

$(document).ready(function(){
function startGame(evt) {
evt.preventDefault();
var answer = Math.floor((Math.random() * 10) + 1);
var guess = $('#guess').val();
var bet = $('#bet').val();
var wallet = $('#wallet');
var display = $('#display');
display.empty();
if (guess == answer) {
display.append('<strong>RIGHT ON THE SHNOZ! WE GOT A WINNER!</strong>');
} else if (guess == (answer + 1) || guess == (answer - 1)) {
display.append('Close, but no cigar! You off by one! (The answer was ' + answer + ')');
} else {
display.append('<em>Sorry, boy, you lost!</em> (The answer was ' + answer + ')');
wallet.text(parseInt(wallet.text()) - bet);
};
};
console.log(wallet.text());
if (parseInt(wallet) < 5) {
$('#placebet').replaceWith("Game over! You suck!");
}
$('#placebet').on('click', startGame);
});
<!DOCTYPE html>
<html>
<head>
<title>betting game</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="game.js"></script>
</head>
<body>
<h1> Betting Game </h1>
<p>
I'm betting $
<input placeholder="how much? [Between 5 and 10 please.]" type="number" id="bet" value="5">
<label>on that there numbah'</label>
<input placeholder="5? maybe 6?" type="number" id="guess" value="5">
<button id="placebet">Place bet</button>
<div id="display">
<p>Place your bets, left, right, and center!</p>
</div>
<div id="wallet">
100
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment