JSFiddle - React, Tailwind, and code Playground
JavaScript
var logMsg = "";
var hitsIfChooseToSwitch = 0, hitsIfChooseNotToSwitch = 0;
var SELECTION = 0;
var TEST_RUNS = 1000000;
var i;
for(i = 0; i < TEST_RUNS; i++){
runTest();
}
log("hitsIfChooseToSwitch: " + hitsIfChooseToSwitch + ", chance is: "
+ hitsIfChooseToSwitch / TEST_RUNS);
log("hitsIfChooseNotToSwitch: " + hitsIfChooseNotToSwitch + ", chance is: "
+ hitsIfChooseNotToSwitch / TEST_RUNS);
function runTest(){
var goats = generateGoats();
var ruledOut = ruleOutOneGoat(goats, SELECTION);
var theOtherOne = ruledOut == 0 ? 1 : 0;
if(theOtherOne == SELECTION){
hitsIfChooseNotToSwitch++;
}else{
hitsIfChooseToSwitch++;
}
}
function generateGoats(){
var goats = [0, 1, 2];
goats.splice(Math.floor(Math.random() * 3),1);
return goats;
}
function ruleOutOneGoat(goats, selection){
if(goats[0] == selection){
return 1;
}else{
return 0;
}
}
function log(msg){
logMsg += "<pre>"+ msg+"</pre>";
document.body.innerHTML = logMsg;
}