const numOfGames = 100 /* Enter the amount of games that should be played before results come in */ // ************************** class LOutcome { constructor(name, value) { this.name = name; this.value = value; this.occurrences = 0; this.probability = 0 } incOccurrences() { this.occurrences++; } updateProbability(games = numOfGames) { this.probability = (this.occurrences / games).toFixed(3); } } const singulars = [ new LOutcome('nugget', 5), new LOutcome('gold', 10), new LOutcome('apple', 15), new LOutcome('emerald', 20), new LOutcome('diamond', 50)]; const consecutiveDoubles = [ new LOutcome('nugget', 14), new LOutcome('gold', 30), new LOutcome('apple', 44), new LOutcome('emerald', 60), new LOutcome('diamond', 150)] const triples = [ new LOutcome('nugget', 150), new LOutcome('gold', 300), new LOutcome('apple', 450), new LOutcome('emerald', 600), new LOutcome('diamond', 1500)] const options = ['nugget', 'nugget', 'nugget', 'gold', 'gold', 'apple', 'emerald', 'diamond'] let prize = 0; function lotteryRoll() { let earnings = 0; let rolls = []; for (let i = 0; i < 3; i++) { const option = options[Math.floor(Math.random() * options.length)] const item = singulars.find(item => item.name === option) item.incOccurrences(); item.updateProbability(numOfGames * 3); rolls.push(item.name); earnings += item.value } if ((rolls[0] === rolls[1]) && (rolls[1] === rolls[2])) { const triple = triples.find(triple => triple.name === rolls[0]) triple.incOccurrences(); triple.updateProbability(); } else if (rolls[0] === rolls[1]) { const double = consecutiveDoubles.find(double => double.name === rolls[0]) double.incOccurrences(); double.updateProbability(); } else if (rolls[1] === rolls[2]) { const double = consecutiveDoubles.find(double => double.name === rolls[1]) double.incOccurrences(); double.updateProbability(); } return earnings; } for (let i = 0; i < numOfGames; i++) { const lotteryPrize = lotteryRoll(); prize += lotteryPrize; } function stringifyOutcomes(arr) { let str = ""; for (item of arr) { str += `name: ${item.name}, value: ${item.value}, occurrences: ${item.occurrences}, probability: ${item.probability}\n` } return str } console.log(`In a total of ${numOfGames} games, the following results came:\n\nSingular slots:\n${stringifyOutcomes(singulars)}\nTwo consecutive slots:\n${stringifyOutcomes(consecutiveDoubles)}\nTriples:\n${stringifyOutcomes(triples)}\n\nThe total amount of tokens rewarded for ${numOfGames} was ${prize} tokens.`);