8000 GitHub - ahmsay/Monty-Hall
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

ahmsay/Monty-Hall

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 

Repository files navigation

Monty-Hall

A demo where you can try the Monty Hall problem. Copy and paste the code to your browser's devtools to try it out !

let winTime = 0
const attempts = 1000

const doors = [
  { position: "left", hasCar: false, chosen: false, open: false },
  { position: "mid", hasCar: true, chosen: false, open: false },
  { position: "right", hasCar: false, chosen: false, open: false }
]

function chooseADoor() {
  const choice = Math.floor(Math.random() * doors.length)
  doors[choice].chosen = true
}

function openADoorThatIsNotChosenAndHasNoCar() {
  const remainingDoors = doors.filter(door => { return !door.chosen && !door.hasCar })
  const randomDoor = Math.floor(Math.random() * remainingDoors.length)
  const doorThatWillBeOpened = doors.indexOf(remainingDoors[randomDoor])
  doors[doorThatWillBeOpened].open = true
}

function chooseTheDoorThatIsNotOpened() {
  return doors.filter(door => { return !door.chosen && !door.open })[0]
}

function resetDoors(doors) {
  doors.forEach(door => {
    door.chosen = false
    door.open = false
  })
}

for (let i = 0; i < attempts; i++) {
  chooseADoor()
  openADoorThatIsNotChosenAndHasNoCar()
  const changedDoor = chooseTheDoorThatIsNotOpened()
  winTime += changedDoor.hasCar
  resetDoors(doors)
}

console.log("You won", winTime, "of", attempts, "attempts.")

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0