Last active
October 29, 2020 17:49
-
-
Save jeanjmichel/61a3ea64f25d4def7727e9f242c3c2ef to your computer and use it in GitHub Desktop.
A JavaScript code to test the Monty Hall Problem.
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
var myBet = Math.floor(Math.random() * 3) + 1; | |
var acceptChangeInitialBet = Math.floor(Math.random() * 2) + 1; | |
function game(initialBet, changeInitialBet) { | |
var portsAndPrizes = []; | |
var carPort = Math.floor(Math.random() * 3) + 1; | |
var newBet; | |
var discardedDoor; | |
var finalGoatDoor; | |
var finalBet; | |
finalBet = initialBet; | |
portsAndPrizes[carPort] = "car"; | |
for(var i = 1; i < 4; i++) { | |
if(i != carPort) | |
portsAndPrizes[i] = "goat"; | |
} | |
function discardingAPossibility() { | |
for(var i = 1; i < 4; i++) { | |
if(i != carPort && i != initialBet) { | |
discardedDoor = i; | |
console.log(`Showing to you that behind door #${discardedDoor} we have a goat!`); | |
break; | |
} | |
} | |
} | |
function printPortsAndPrizes() { | |
for(var i = 1; i < 4; i++) { | |
console.log(`Behind door #${i} we have: ${portsAndPrizes[i]}`); | |
} | |
} | |
function changeInitialBet(initialBet) { | |
for(var i = 1; i < 4; i++) { | |
if(i != initialBet && i != discardedDoor) { | |
newBet = i; | |
finalBet = newBet; | |
console.log(`Your new bet is in the door #${newBet}, good luck!`); | |
break; | |
} | |
} | |
} | |
console.log(`Your initial bet is in the port #${initialBet}`); | |
//printPortsAndPrizes(); | |
discardingAPossibility(); | |
console.log("Do you want change your inicial bet?"); | |
console.log(`Answer: ${acceptChangeInitialBet}`); | |
if(acceptChangeInitialBet == 1) | |
changeInitialBet(initialBet); | |
if(finalBet == carPort) | |
console.log("Congratulations, you win a new brand car!"); | |
else | |
console.log("Congratulations, you win an old smelly goat!"); | |
} | |
game(myBet, acceptChangeInitialBet); | |
/* | |
Console log: | |
"Your initial bet is in the port #1" | |
"Showing to you that behind door #2 we have a goat!" | |
"Do you want change your inicial bet?" | |
"Answer: 2" | |
"Congratulations, you win an old smelly goat!" | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Recently appears a lot of entries in the Medium about Monty Hall Problem, some impassioned by a Brooklyn Nine-Nine episode.
This code in JavaScript demonstrate how the problem work:
Imagine that you were invited to a TV game where you will try win a car.
To do it you have to choose a door of three doors.
The presenter of the game, after you choose, open a door and show to you a goat, and offers to you a chance to change your initial bet.
At this point, changing your mind and choosing another door, your changes of win a new car will increase or decrease?
Will increase!
Your expectative of win will be 2/3 not 1/3.
Run this code sometimes and you will see this, 66% of incensements when you change the initial bet!