Friday 13th

by Pratik Bhonsle

HTML

<div class="intro">All the years that have a Friday the 13th</div>
<select name="month" id="month">
    <option value="0">Jan</option>
    <option value="1">Feb</option>
    <option value="2">Mar</option>
    <option value="3">Apr</option>
    <option value="4">May</option>
    <option value="5" selected="SELECTED">Jun</option>
    <option value="6">Jul</option>
    <option value="7">Aug</option>
    <option value="8">Sep</option>
    <option value="9">Oct</option>
    <option value="10">Nov</option>
    <option value="11">Dec</option>
</select>
<input type="text" name="year" id="year" placeholder="1986." />
<input type="text" name="age" id="age" placeholder="Age 100." />
<button type="button" id="calculate">Calculate</button>
<div id="output"></div>

CSS

#output {
    font-family:"courier";
    font-size: 0.9em;
    font-weight: bold;
    height: 250px;
    overflow-y: scroll;
}

JavaScript

(function () {
    $(document).on('click', '#calculate', function () {
        var output = getFridayThe13s($('#year>option:selected').val(), $('#month').val(), $('#age').val());
        $('#output').html(output);
    });
})();

function getFridayThe13s(year, month, age) {
    //debugger;
    var year = last = year || 1986,
        month = month || 5,
        age = age || 100,
        date = new Date(year, month, 13),
        output = [];
    for (var i = 0; i <= age; i++) {
        date.setFullYear(year + i);
        if (date.getDay() == 5) {
            var find = date.getFullYear();
            output.push(find + ' :: ' + (find - last) + '<br/>');
            last = find;
        }
    }
    return output;
}