cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <head>
  	<link href='https://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
  
<body id="bohday">
  <h1>The Monty Hall Problem :)</h1>
  <table id="doorsTable">
<tr>
  <td>
    <label for="door1">
      <div class="yay">
        <div class="door">.</div>
        <div class="meh">?</div>
      </div>
    </label>
  </td>
  <td>
    <label for="door2">
      <div class="yay">
        <div class="door">.</div>
        <div class="meh">?</div>
      </div>
    </label>
  </td> 
  <td>
    <label for="door3">
      <div class="yay">
        <div class="door">.</div>
        <div class="meh">?</div>
      </div>
    </label>
  </td>
</tr>
<tr>
  <td><input type="radio" name="door" id="door1" value=0><label for="door1">Door 1</label></td>
  <td><input type="radio" name="door" id="door2" value=1><label for="door2">Door 2</label></td> 
  <td><input type="radio" name="door" id="door3" value=2><label for="door3">Door 3</label></td>
</tr>
</table>
  
  <center>or<br>
  <input type="radio" name="door" id="random" value="Randomise!" checked><label for="random">Randomise!</label></center></p>
  
  <input id="switch" type="checkbox" checked><label for="switch">Always switch</label></input></p>

<label for="input">Number of games</label> <input id="input" placeholder="Number of games" value="10"/> </p>

<p align="right"><button onclick="newGame()">Run Games</button></p>
<hr>
<h3><div id="wins">┗(-_-)┛┏(-_-)┓┏(-_-)┛┗(-_- )┓┗(-_-)┛┏(-_-)┓</div>
  <div id="totalGames">┏(-_-)┓┏(-_-)┛┗(-_- )┓┗(-_-)┛┏(-_-)┓┏(-_-)┛</div></h3>
<h1><div id="winRate">┗(-_-)┛┏(-_-)┓┏(-_-)┛┏(-_-)┓</div></h1>
<div id="console">
  Suppose you're on a game show, and you're given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what's behind the doors, opens another door, say No. 3, which has a goat. He then says to you, "Do you want to pick door No. 2?" Is it to your advantage to switch your choice?<br>
<a href="http://en.wikipedia.org/wiki/Monty_Hall_problem">http://en.wikipedia.org/wiki/Monty_Hall_problem</a>
</div>
<p align="right"><button onclick="beepboop()">Clear</button></p>

</body>
            
          
!
            
              #bohday {
		font-family: 'Open Sans', 'sans-serif';
		background-color: skyBlue;
		color: white;
        max-width: 700px;
        margin-left:auto;
        margin-right:auto;
		}

#wins, #totalGames, #winRate   {
        white-space: nowrap
}

a       {
        color:white;
}

#doorsTable   {
        width:100%;
        text-align:center;
}

.yay    {
        position:relative;
        width:100px;
        height:180px;
        margin-left:auto;
        margin-right:auto;
}

.door   {
        width:100px;
        height:180px;
        margin:20px;
        background:brown;
        color: white;
        line-height:140px;
        text-align:right;
        font-size:100px;
        margin-left:auto;
        margin-right:auto;
}

.door:hover {
    -webkit-animation: open 0.75s ease;
}


@-webkit-keyframes open {
    from {
       -webkit-transform: perspective(300) rotateY(0deg);
       -webkit-transform-origin: 0% 0%;
    }
    to {
       -webkit-transform: perspective(300) rotateY(-180deg);
       -webkit-transform-origin: 0% 50%;
    }
}

.meh    {
        width:100px;
        height:180px;
        top:-5px;
        left:-5px;
        position:absolute;
        z-index:-1;
        background:black;
        text-align:center;
        vertical-align: middle;
        line-height: 180px;
        color: white;
        font-size: 50px;
        margin-left:auto;
        margin-right:auto;
        border:solid;
        border-width:5px;
        border-bottom:0px;
        border-color:gold;
}

#console {
		overflow-y: scroll;
		height: 190px;
        border: solid;
        border-width:1px;
        background-color:rgba(255,255,255,0.2);
		}
            
          
!
            
              // Made by Mei Weng Brough-Smyth
// https://www.beepboop.com.au/2014/05/the-monty-hall-problem.html

var doorSelected;
var willSwitch;
var noOfGames;
var input;

var doorOpened = 0;
var timesWon = 0;
var totalGames = 0;

var consoleOut = document.getElementById("console");

function newGame() {
  // Get WillSwitch input
  willSwitch = (document.getElementById("switch").checked)? true : false;
  
  // Get Radio input
  var radios = document.getElementsByName('door');
  for (var i = 0, length = radios.length; i < length; i++)   {
    if (radios[i].checked) {
        doorSelected = radios[i].value;
        break;
    }
  }
  if (doorSelected === "Randomise!") {
    doorSelected = Math.floor(Math.random() * 3);
  }
  doorSelected = parseInt(doorSelected, 10);
  
  // Get number of Games
  input = document.getElementById('input').value;
  noOfGames = input.match(/^\d+$/);
  
  // Get random winning door
  var winningDoor = Math.floor(Math.random() * 3);

  // Initialise game
  var game = [false, false, false];
  game[winningDoor] = true;
  
  // Initialise player perspective
  var player = [null, null, null];
  
  player[doorSelected] = "doorSelected";
  
  // Door select output
  doorSelectedOut = doorSelected + 1;
  consoleOut.innerHTML += "You select door " + doorSelectedOut + ".<br>";
  
  // Find door door to open
  doorOpened = doorSelected + 1;
  if (doorOpened > 2) { //If it's too high, bring it back down into our array
     doorOpened = doorOpened - 3;
  }
  if (game[doorOpened] === true) { //Move opened door along if it's the winning door
     doorOpened++;
  }
  
  // Door opened output and append to player array
  doorOpenedOut = doorOpened + 1;
  consoleOut.innerHTML += "Door " + doorOpenedOut + " is opened to reveal nothing.<br>";
  player[doorOpened] = "doorOpened";
  
  if (willSwitch === true) {
    for (var j = 0; j < player.length; j++) {
      if (player[j] === null) {
        // Get player to switch to null in player array and output
        jOut = j+1;
        consoleOut.innerHTML += ("You switch to door " + jOut + ".<br>");
        if (game[j] === true) {
          timesWon++; // Check if switched door is this winning door
          consoleOut.innerHTML += ("<b>You win!<br></b>");
        } else {
          consoleOut.innerHTML += ("<b>You lose.<br></b>");
        }
      }
    }
  } else {
    for (var k = 0; k < player.length; k++) {
      if (player[k] === "doorSelected") {
        consoleOut.innerHTML += "You stick with " + doorSelectedOut + "<br>";
        if (game[doorSelected] === true) {
          timesWon++; // Check if initial door is this winning door
          consoleOut.innerHTML += ("<b>You win!<br></b>");
        } else {
          consoleOut.innerHTML += ("<b>You lose.<br></b>");
        }
      }
    }
  }
  
  console.log(game);
  console.log(player);
  
  totalGames++;
  
  percentageWon = (timesWon/totalGames*100).toFixed(0) + "%";
  document.getElementById("wins").innerHTML = "Total wins: " + timesWon;
  document.getElementById("totalGames").innerHTML = "Total games: " + totalGames;
  document.getElementById("winRate").innerHTML = "Win rate: " + percentageWon;
  consoleOut.innerHTML += "----------------------------------------------------------<br>";
  if (totalGames < noOfGames) {
    newGame();
  } else {
    consoleOut.innerHTML += "<b>End game number " + totalGames + "! \\(^w^)/ </b><br>";
    reset();
  }
}

function reset() {
  timesWon = 0;
  totalGames = 0;
  consoleOut.scrollTop = consoleOut.scrollHeight;
}

function beepboop() {
  consoleOut.innerHTML = "";
  document.getElementById("console").innerHTML = "Suppose you're on a game show, and you're given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what's behind the doors, opens another door, say No. 3, which has a goat. He then says to you, \"Do you want to pick door No. 2?\" Is it to your advantage to switch your choice?<br><a href=\"http://en.wikipedia.org/wiki/Monty_Hall_problem\">http://en.wikipedia.org/wiki/Monty_Hall_problem</a>";
  document.getElementById("wins").innerHTML = "┗(-_-)┛┏(-_-)┓┏(-_-)┛┗(-_- )┓┗(-_-)┛┏(-_-)┓";
  document.getElementById("totalGames").innerHTML = "┏(-_-)┓┏(-_-)┛┗(-_- )┓┗(-_-)┛┏(-_-)┓┏(-_-)┛";
  document.getElementById("winRate").innerHTML = "┗(-_-)┛┏(-_-)┓┏(-_-)┛┏(-_-)┓";
}

            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console