This repository has been archived by the owner on Sep 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
109 lines (94 loc) · 3.36 KB
/
index.js
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
let currentDate = null;
let difficultiy;
const monthNames = [
'January', 'February', 'March',
'April', 'May', 'June', 'July',
'August', 'September', 'October',
'November', 'December'
];
const weekDayNames = [
'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'
];
const randomDate = (start, end) => {
return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
};
const difficulties = [
{start: new Date(new Date().getFullYear(), 0, 1), end: new Date(new Date().getFullYear(), 11, 31)},
{start: new Date(new Date().getFullYear(), 0, 1), end: new Date(new Date().getFullYear() + 1, 11, 31)},
{start: new Date(2000, 0, 1), end: new Date(2099, 11, 31)},
{start: new Date(1900, 0, 1), end: new Date(2099, 11, 31)},
{start: new Date(1600, 0, 1), end: new Date(2099, 11, 31)},
{start: new Date(100, 0, 1), end: new Date(2999, 11, 31)}
];
const getRandomDateByDifficulty = () => {
return randomDate(difficulties[difficultiy - 1].start, difficulties[difficultiy - 1].end);
};
const dateToString = (d) => {
return d.getDate() + ' ' + monthNames[d.getMonth()] + ' ' + d.getFullYear();
};
const alertCorrect = document.getElementById('alertCorrect');
const alertWrong = document.getElementById('alertWrong');
const dateSpan = document.getElementById('dateSpan');
const nextButton = document.getElementById('next');
const dayOptionsContainer = document.getElementById('day-options-container');
const weekDayButtons = [
document.getElementById('btnSunday'),
document.getElementById('btnMonday'),
document.getElementById('btnTuesday'),
document.getElementById('btnWednesday'),
document.getElementById('btnThursday'),
document.getElementById('btnFriday'),
document.getElementById('btnSaturday')
];
const ask = (diff) => {
difficultiy = diff;
document.getElementById('container-level-choice').style.display = 'none';
document.getElementById('container-ask').style.display = 'block';
refresh();
};
const refresh = () => {
alertCorrect.style.display = 'none';
alertWrong.style.display = 'none';
dayOptionsContainer.classList.remove("result-alert-shown");
currentDate = getRandomDateByDifficulty();
dateSpan.innerHTML = dateToString(currentDate);
};
const checkAnswer = (ans) => {
for (const b of weekDayButtons) {
b.setAttribute('disabled', 'true');
}
nextButton.style.display = 'inline';
nextButton.focus();
if (ans === currentDate.getDay()) {
alertCorrect.innerHTML = `<span style="font-size: 2.7em;">✅</span><br />Correct!<br />
Answer: ${ weekDayNames[ans] }`;
alertCorrect.style.display = 'block';
} else {
alertWrong.innerHTML = `<span style="font-size: 2.7em;">❌</span><br />Wrong!<br />
Date: ${ dateToString(currentDate) },<br />
Your answer: ${ weekDayNames[ans] },<br />
Correct answer: ${ weekDayNames[currentDate.getDay()] }`;
alertWrong.style.display = 'block';
}
dayOptionsContainer.classList.add("result-alert-shown");
};
for (let i = 0; i < weekDayButtons.length; i++) {
weekDayButtons[i].addEventListener('click', () => {
checkAnswer(i);
});
}
nextButton.addEventListener('click', () => {
for (const b of weekDayButtons) {
b.removeAttribute('disabled');
}
nextButton.style.display = 'none';
refresh();
});
window.addEventListener('keydown', (keyEvent) => {
if (!isNaN(keyEvent.key)) {
const number = parseInt(keyEvent.key);
if (number <= 7) {
checkAnswer(number % 7);
}
}
});