3 ms·
Hah, I wrote one as well to prove it to myself in the chrome dev tools console const pickCar = (switchChoice) => { const doors = [0, 0, 0]; doors[(Ma
by reidjs 7y ago
Hah, I wrote one as well to prove it to myself in the chrome dev tools console
const pickCar = (switchChoice) => {
const doors = [0, 0, 0];
doors[(Math.random() * doors.length - 1) | 0] = 1
let pick = (Math.random() * doors.length - 1) | 0
let goat
for(let i = 0; i < doors.length; i++) { if (doors[i] == 0 && i != pick) goat = i }
let result = pick
if (switchChoice) for(let i = 0; i < doors.length; i++) { if (i != pick && i != goat) result = i }
return result
}
let switchWins = 0;
let switchLosses = 0;
for(let i = 0; i < 100000; i++) {
pickCar(true) == 1 ? switchWins++ : switchLosses++
}
console.log('wins:', switchWins, ', losses: ',switchLosses)