-
Notifications
You must be signed in to change notification settings - Fork 0
/
birthday.html
70 lines (61 loc) · 2.52 KB
/
birthday.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Birthday Weekdays</title>
</head>
<body>
<p>
Have you ever wondered what weekdays your birthdays fell on?
</p>
<p>
No? Well me neither. But if you ever want to know, here you go:
</p>
<form>
<label for="bd">Your birthday:</label>
<input id="bd" type="date" placeholder="Your Birthday" required />
<button>SHOW ME</button>
</form>
<div id="results">
</div>
<script>
document.querySelector("form").addEventListener("submit", ev => {
ev.preventDefault();
const bdInput = document.getElementById("bd");
const results = document.getElementById("results");
results.innerHTML = "";
const birthday = new Date(bdInput.valueAsNumber);
let weekdayOccurences = [0, 0, 0, 0, 0, 0, 0];
const weekdayNames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
let i = 0;
let date = birthday;
const birthdayList = document.createElement("ul");
while (date.getFullYear() <= new Date().getFullYear()) {
const listItem = document.createElement("li");
listItem.innerText = i + ": " + date.toLocaleDateString(undefined, {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric"
});
birthdayList.appendChild(listItem);
weekdayOccurences[date.getDay()] += 1;
i += 1;
date = new Date(date.getFullYear() + 1, date.getMonth(), date.getDate());
}
const weekdayAggregation = document.createElement("ul");
for (let i = 0; i < weekdayOccurences.length; i++) {
const listItem = document.createElement("li");
listItem.innerText = `${weekdayNames[i]}: ${weekdayOccurences[i]} times`;
weekdayAggregation.appendChild(listItem);
}
results.appendChild(document.createElement("hr"));
results.appendChild(document.createTextNode("Birthday occurances per weekday:"))
results.appendChild(weekdayAggregation);
results.appendChild(document.createTextNode("All your birthdays:"))
results.appendChild(birthdayList);
});
</script>
</body>
</html>