JSFiddle - React, Tailwind, and code Playground
by fenderistic
HTML
<p>When they switched: <div id="switch"></div></p>
<p>When they stayed: <div id="stayed"></div></p>
JavaScript
var times =100000;
var count = 1;
var goat1;
var goat2;
var car;
var doors=new Array();
var firstDoor;
var secondDoor;
var removedDoor;
var decision;
var wonWhenSwitched=0;
var wonWhenStayed=0;
var myVar = setInterval(function(){start();},300);
function setPos(){
//set goat 1
goat1=Math.floor((Math.random()*3)+1)-1;
doors[goat1]="g";
//set goat 2 where goat 1 isn't
goat2=Math.floor((Math.random()*3)+1)-1;
while(goat2==goat1){
goat2=Math.floor((Math.random()*3)+1)-1;
}
doors[goat2]="g";
//set car in last open spot
car=Math.floor((Math.random()*3)+1)-1;
while(car == goat1 || car == goat2 ){
car=Math.floor((Math.random()*3)+1)-1;
}
doors[car]="c";
}
function start(){
// for(var i=0;i<times;i++){
setPos();
//contestant picks first door
firstDoor =Math.floor((Math.random()*3)+1)-1;
//host removes one door that has a goat behind it
removedDoor = Math.floor((Math.random()*3)+1)-1;
while(firstDoor==removedDoor || doors[removedDoor]=="c"){
removedDoor = Math.floor((Math.random()*3)+1)-1;
}
doors[removedDoor]="x";
//contestant decides to stay w/ same door or switch
decision = Math.floor((Math.random()*2)+1)-1;
if(decision){
secondDoor = Math.floor((Math.random()*3)+1)-1;
while(secondDoor==removedDoor || secondDoor == firstDoor){
secondDoor = Math.floor((Math.random()*3)+1)-1;
}
if(doors[secondDoor]=="c"){wonWhenSwitched++;} else {wonWhenStayed++;}
} else{
if(doors[firstDoor]=="c"){wonWhenStayed++;} else {wonWhenSwitched++;}
}
// }
$("#switch").html(wonWhenSwitched + "/" + count + " = %" + (wonWhenSwitched/count)*100);
$("#stayed").html(wonWhenStayed + "/" + count + " = %" + (wonWhenStayed/count)*100);
count++;
}