Calculate and display car switching probabilities in a game.

HTML

Uninformed-reveal modification to the MH problem:<br>
Please wait while simulating...<br>

CSS

body {
  font-family: Arial, Helvetica, sans-serif;
  color: yellowgreen;
  background: black;
}

JavaScript

window.onload = () => {

const N = 100000000

let nDecisions = 0
let nCarsSwitching = 0
let nCarsNotSwitching = 0

for (let i = 0; i < N; ++i) {
  const carLocation = Math.floor(Math.random() * 3)
  const initialPlayerChoice = 0
  const randomlyRevealedLocation = Math.floor(Math.random() * 2) + 1
  
  if (randomlyRevealedLocation !== carLocation) {
    ++nDecisions

    if (initialPlayerChoice === carLocation) {
      ++nCarsNotSwitching
    }
    else {
      ++nCarsSwitching
    }
  }
}

log('Num simulated rounds: ' + (N))
log('Num simulated player decisions: ' + (nDecisions))

log('Probability of winning the car...')
log('A) ...if switching: ' + (nCarsSwitching / nDecisions))
log('B) ...if NOT switching: ' + (nCarsNotSwitching / nDecisions))

} //window.onload

function log(s) {
  document.body.innerText += s
  document.body.innerHTML += '<br>'
}