JSFiddle - React, Tailwind, and code Playground
HTML
<input type="button" id="start" value="Start" />
<div id="output"></div>
JavaScript
var SIMULATIONS = 10000;
var TURNS = 5; // Number of flies to pass by
var MAX = 3; // Number of flies to get full
var
CATCH = 0.5; // Probability to catch fly
var total = 0; // Times simulation was run
var wins = 0; // Times spider got full
var btnStart = document.getElementById("start");
var btnStop = document.getElementById("stop");
var output = document.getElementById("output");
var history = document.getElementById("history");
btnStart.addEventListener('click', start);
btnStart.addEventListener('click', start);
function simulate() {
deaths = 0;
total++;
for (var i = 1; i <= TURNS; i++) {
if (Math.random() >
CATCH) {
deaths++;
if (deaths == MAX) {
wins++;
break;
}
}
}
}
function start() {
var runs = SIMULATIONS;
while (runs-- > 0) {
simulate();
}
update();
}
function status() {
var percent = Math.round(100 * wins / total) / 100;
return percent + "% (" + wins + " / " + total + ")";
}
function update() {
var stats = status();
output.innerHTML = stats;
}