Skip to content

Instantly share code, notes, and snippets.

@avibryant
Last active December 23, 2015 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avibryant/6680319 to your computer and use it in GitHub Desktop.
Save avibryant/6680319 to your computer and use it in GitHub Desktop.
<html>
<head>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script>
$(function() {
$( "#datepicker" ).datepicker(
{onSelect: function(d,o){
dueDate($.datepicker.parseDate("mm/dd/yy", d))
}}
);
});
var DATA = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 2, 1, 4, 2, 2, 1, 4, 6, 7, 5, 1, 5, 8, 7, 9, 10, 11, 13, 13, 18,
14, 13, 9, 27, 29, 27, 31, 27, 26, 36, 43, 43, 51, 67, 74, 60, 47,
41, 44, 36, 37, 48, 30, 20, 22, 24, 16, 11, 12, 8, 4, 2, 0, 1, 0, 1
]
Date.prototype.addDays = function(n) {
var d = new Date();
d.setTime(this.getTime() + (n * 1000 * 60 * 60 * 24));
return d;
}
function dueDate(due) {
var startIndex = Math.floor((new Date().addDays(58) - due) / (1000 * 60 * 60 * 24));
var cumulativeFreq = []
var acc = 0;
var i = startIndex;
while(i < DATA.length) {
acc += DATA[i];
cumulativeFreq[i-startIndex] = acc;
i += 1;
}
var totalFreq = cumulativeFreq[DATA.length - startIndex - 1];
if(totalFreq > 0) {
var cumulativeProb = [];
var i = 0;
while(i < cumulativeFreq.length) {
cumulativeProb[i] = cumulativeFreq[i] / totalFreq;
i += 1;
}
render(cumulativeProb);
} else {
$("#output").html("Baby isn't coming any time soon. Check back later.")
}
}
function render(prob) {
var text = "<p><b>Chance of baby</b><table>";
var i = 0;
while(i < prob.length) {
text += "<tr><td>by ";
text += $.datepicker.formatDate("DD, MM d", new Date().addDays(i));
text += "</td><td>" + Math.floor(prob[i] * 100) + "%</td></tr>";
i += 1;
}
text += "</table>";
$("#output").html(text);
}
</script>
</head>
<body>
<p>
Everyone knows that the baby is unlikely to come on the exact due date, but it's not usually very clear what the chances are that he or she will arrive by a given date. Also, these chances change over time: for example, at first it's pretty unlikely that the baby will come on the day two weeks after the due date, but if you're already 10 days late with no baby, those chances are suddenly a lot higher.
</p>
<p>
Enter your due date below to see the current probabilities. Come back to check again later to see how they change over time.
</p>
Due date: <input type="text" id="datepicker" />
<div id="output" />
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment