JSFiddle - React, Tailwind, and code Playground
JavaScript
var wins = 0,
losses = 0,
switchDoors = confirm('In this test, should the user switch doors?'),
doors, x, y, choice, removed;
for(x=0;x<1000;x++) {
// Put the car behind the winning door
doors = [0, 0, 0];
doors[Math.floor(Math.random() * 3)] = 1;
// Contestant, choose a door!
choice = Math.floor(Math.random() * 3);
// Host removes a door from the game
removed = doors.slice(0);
removed.splice(choice, 1);
if (removed[0] === 0 && removed[1] === 1) {
if (choice !== 0) removed = 0;
else removed = 1;
} else if (removed[0] === 1 && removed[1] === 0) {
if (choice !== 2) removed = 2;
else removed = 1;
} else {
removed = Math.floor(Math.random() * 2);
if (choice === 0 || (choice === 1 && removed === 1)) removed++;
}
doors.splice(removed, 1);
if (choice === 2 || removed === 0) choice--;
// Contestant sticks with their door, or changes their choice
if (switchDoors) choice = choice^1;
if (doors[choice] === 1) wins++;
else losses++;
}
alert('Wins: '+wins+', Losses: '+losses+', Win Rate: '+(wins/10)+'%');