/* 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)) {
console.log("The day " + checkDate + " is not Alan Turing's birthday");
}
console.log("Let's celebrate, " + checkDate + " will be a great day!");
/* 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());
// Convert back to days and return
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.