Deriving e

From this reddit post: https://www.reddit.com/r/AskReddit/comments/6il1jx/whats_the_coolest_mathematical_fact_you_know_of/dj78wzt/

by Nathan Friend

HTML

<button id="start-button">Click me to start calculating</button>
<pre id="output"></pre>

JavaScript

$('#start-button').click(function() {
  $('#output').html('Working...');
  setTimeout(function() { calculateE(10000000); });
});

function calculateE(iterations) {
  var counts = [];
  for (var i = 0; i < iterations; i++) {
    var sum = 0;
    var count = 0;
    while (sum < 1) {
      sum += Math.random();
      count++;
    }
    counts.push(count);
  }

  var average = counts.reduce(function(acc, val) {
    return acc + val;
  }) / counts.length;

  $('#output').html('Average of ' + iterations + ' iterations: ' + average);
}