Created
August 18, 2024 14:15
-
-
Save joeljuca/9e5738dbdbeaf035588011785d0fdd5a to your computer and use it in GitHub Desktop.
A highly questionable (from an ethical POV) implementation of a slot machine
This file contains hidden or 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
var SlotMachine = (function () { | |
//prettier-ignore | |
var invalidBetError = new Error("Bet should be a number between 1 and 10"); | |
/** | |
* @params params Object { maxRounds: number, maxWins: number } | |
*/ | |
var SlotMachine = function (params) { | |
var winCount = 0; | |
var roundCount = 0; | |
var maxRounds = (params && params.maxRounds) || 10; | |
var maxWins = (params && params.maxWins) || Math.ceil(maxRounds / 10); | |
return function (bet) { | |
if (typeof bet !== "number") throw invalidBetError; | |
bet = Math.floor(bet); | |
if (bet < 1 || bet > 10) throw invalidBetError; | |
do { | |
var draw = Math.floor(Math.random() * 10 + 1); | |
} while (winCount === maxWins && bet === draw); | |
roundCount++; | |
if (roundCount === maxRounds) { | |
winCount = 0; | |
roundCount = 0; | |
} | |
if (bet === draw) { | |
return { win: true, bet: bet, draw: draw, message: "You won!" }; | |
} else { | |
return { win: false, bet: bet, draw: draw, message: "You lost." }; | |
} | |
}; | |
}; | |
return SlotMachine; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment