-
-
Save srsgores/8417805467ffc1cdb6b8bf321c6cd73b to your computer and use it in GitHub Desktop.
Sample implementation of the betting game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const STARTING_CASH = 100; | |
const GUESS_MIN = 1; | |
const GUESS_MAX = 10; | |
const BET_MIN = 5; | |
const BET_MAX = 10; | |
function getComputerGuess() { | |
return Math.floor(Math.random() * GUESS_MIN - GUESS_MAX); | |
} | |
function getPlayerBet() { | |
let bet = prompt(`How much will you bet?`); | |
let parsedBet = parseInt(bet); | |
if (parsedBet < GUESS_MIN || parsedBet > BET_MAX) { | |
alert(`Your bet ${parsedBet} is not a valid bet. It is not between ${BET_MIN} and ${BET_MAX}`); | |
return getPlayerBet(); | |
} | |
return parsedBet; | |
} | |
function promptNumber() { | |
return prompt(`I'm thinking of a number between ${GUESS_MIN} and ${GUESS_MAX}`); | |
} | |
function isCorrect(bet, actual) { | |
return bet === actual || (bet === (actual - 1) || bet === (actual + 1)); | |
} | |
function start() { | |
let turn = 1; | |
let playerCash = STARTING_CASH; | |
while (playerCash > 0) { | |
let computerGuess = getComputerGuess(); | |
playerBet = getPlayerBet(); | |
let playerGuess = promptNumber(); | |
if (!isCorrect(playerGuess, computerGuess)) { | |
playerCash = playerCash - playerBet; | |
alert(`Wrong answer! Guessed ${playerGuess}. Remaining cash: ${playerCash}`); | |
} | |
turn++; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment