JSFiddle - React, Tailwind, and code Playground

by Andy Mcloughlin

HTML

<body>
    <article></article>
</body>

CSS

article {
    display:none;
    opacity:0;
}

JavaScript

/* All you need to get 100% */

function isItAlansBirthday(date) {
    var birthday = new Date(1912, 5, 23);
    return birthday.getDate() == date.getDate() && birthday.getMonth() == date.getMonth();
}

function isTodayAlansBirthday() {
    return isItAlansBirthday(new Date());
}

for (var checkDate = new Date(); !isItAlansBirthday(checkDate); checkDate.setDate(checkDate.getDate() + 1)) {
    $("article").before("<p>The day " + checkDate + " is not Alan Turing's birthday</p>");
}
$("article").before("<h1>Let's celebrate, " + checkDate + " will be a great day!</h1>");
/* End of answers */


// Bonus Q: this is simple if you did q3, since all you need to do is put that loop in a function and start a counter
function howManyDaysUntilAlansBirthday() {
    var days = 0;
    for (var checkDate = new Date(); !isItAlansBirthday(checkDate); checkDate.setDate(checkDate.getDate() + 1)) {
        days++;
    }
    return days;
}

var daysUntilBDay = howManyDaysUntilAlansBirthday();
if (daysUntilBDay) {
    $("article").before("<p>" + daysUntilBDay + " days until birthday.");
} else {
    $("article").before("<p>It's Alan's birthday!</p>");
}

/*
 * Here's a faster-running function I
 * made by just copying code someone 
 * else on StackOverflow had written. 
 * It's much more complex and a lot more
 * work to write. 
 */
function fast_howManyDaysUntilAlansBirthday() {
    var birthday = new Date(1912, 5, 23);
    var today = new Date();
    today.setHours(0);
    today.setMinutes(0);
    today.setMilliseconds(0);
    // The number of milliseconds in one day
    var ONE_DAY = 1000 * 60 * 60 * 24;
    // Check if it's this year or next year
    if (birthday.getMonth() < today.getMonth() && birthday.getDate() < today.getDate()) {
        birthday.setFullYear(today.getFullYear() + 1);
    } else {
        birthday.setFullYear(today.getFullYear());
    }
    // Calculate the difference in milliseconds
    var difference_ms = Math.abs(birthday.getTime() - today.getTime());
    //...