xkcd 1930
JavaScript Implementation of xkcd 1930
HTML
<button id="gen">Generate</button>
<div id="content"></div>
CSS
#content {
background-color: #fff;
padding: 1em;
width: 20em;
margin-top: 1em;
}
JavaScript
function xkcd1930() {
function concat(arr) {
return function() {
var parts = [];
arr.forEach(function(el) {
while (typeof el !== 'string') {
el = el();
}
parts.push(el);
});
return parts.join('');
};
}
function choose(arr) {
return function() {
return arr[Math.floor(Math.random() * arr.length)];
};
}
return concat([
'Did you know that ',
choose([
concat([
'the ',
choose(['fall','spring']),
' equinox'
]),
concat([
'the ',
choose(['winter','summer']),
' ',
choose(['solstice','olympics'])
]),
concat([
'the ',
choose(['earliest','latest']),
' ',
choose(['sunrise','sunset'])
]),
concat([
'daylight ',
choose(['saving','savings']),
' time'
]),
concat([
'leap ',
choose(['day','year'])
]),
'Easter',
concat([
'the ',
choose(['harvest','super','blood']),
' moon'
]),
'Toyota Truck Month',
'Shark Week'
]),
' ',
choose([
concat([
'happens ',
choose(['earlier','later','at the wrong time']),
' every year'
]),
concat([
'drifts out of sync with the ',
choose([
'sun',
'moon',
'zodiac',
concat([
choose(['Gregorian','Mayan','lunar','iPhone']),
' calendar'
]),
'atomic clock in Colorado'
])
]),
concat([
'might ',
choose(['not happen','happen twice']),
' this year'
])
]),
' because of ',
choose([
concat([
'time zone legislation in ',
choose(['Indiana','Arizona','Russia'])
]),
'a decree by the Pope in the 1500s',
concat([
...