Created
July 11, 2010 06:47
-
-
Save seanmonstar/471345 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<label> | |
<span>Always switch:</span> | |
<input type="checkbox" name="change" id="change" /> | |
</label> | |
<button type="button" id="btn">Loop</button> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var gameshow = (function() { | |
var doors = [1, 0, 0]; | |
return function(change) { | |
doors.shuffle(); | |
var picked = 0, //TODO- make random? | |
prize = doors.indexOf(1), | |
revealed; | |
if(picked == prize) { | |
revealed = $random(1, 2); | |
} else { | |
if(prize == 1) { | |
revealed = 2; | |
} else if (prize == 2) { | |
revealed = 1; | |
} | |
} | |
if(change) { | |
picked = revealed > 1 ? 1 : 0; | |
} | |
return picked == prize; | |
}; | |
})(); | |
var loop = function(times, change) { | |
var attempts = times, | |
wins = 0; | |
for(var i = 0; i < attempts; i ++) { | |
wins += gameshow(change) ? 1 : 0; | |
}; | |
return wins; | |
//alert('Wins: '+wins+', Attempts: '+attempts+', Percent: '+(wins/attempts)); | |
} | |
var change = false, | |
changeToggle = $('change'); | |
changeToggle.addEvent('click', function() { | |
change = !change; | |
var msg = new Element('p', { 'html': '------ Changed to <b>' + (change ? 'Always' : 'Never') + '</b> Switch ---------' }); | |
msg.inject(document.body); | |
}); | |
$('btn').addEvent('click', function() { | |
var attempts = 1000, | |
wins = loop(1000, change); | |
var para = new Element('p', { 'text': 'Wins: '+wins+', Attempts: '+attempts+', Percent: '+(wins/attempts) }); | |
para.inject(document.body); | |
}); | |
changeToggle.inject(document.body); | |
btn.inject(document.body); | |
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment