-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
75 lines (66 loc) · 2.44 KB
/
Copy pathscript.js
File metadata and controls
75 lines (66 loc) · 2.44 KB
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
const arrow = document.querySelector(".arrow");
const dayInput = document.querySelector("#dd");
const monthInput = document.querySelector("#mm");
const yearInput = document.querySelector("#yy");
const empty = document.querySelectorAll(".empty");
const allInputs = document.querySelectorAll(".input");
const label = document.querySelectorAll("label");
const small = document.querySelectorAll("small");
const yearsresult = document.querySelector(".yearsLived");
const monthsresult = document.querySelector(".monthsLived");
const daysresult = document.querySelector(".daysLived");
function onClickArrow() {
const day = Math.abs(dayInput.value);
const month = Math.abs(monthInput.value);
const year = Math.abs(yearInput.value);
// ------------------ if the form is empty
if (day == "" || month == "" || year == "") {
empty.forEach((e) => {
e.style.display = "block";
allInputs.forEach((event) => {
event.style.border = "1px solid red";
});
label.forEach((e) => {
e.style.color = "red";
});
return;
});
} else {
empty.forEach((e) => {
e.style.display = "none";
allInputs.forEach((event) => {
event.style.border = "1px solid hsl(0, 0%, 86%)";
});
label.forEach((e) => {
e.style.color = "hsl(0, 1%, 44%)";
});
});
}
//------------------------------------ calculation of age----------
const pastDate = new Date(year, month - 1, day);
const date = new Date();
const totalDays = (date - pastDate) / (1000 * 60 * 60 * 24);
// calcule years lived
const yearsLived = Math.floor(totalDays / 365.25);
const remainingDaysAfterYears = totalDays - yearsLived * 365.25;
// calcule Months
const monthsLived = Math.floor(remainingDaysAfterYears / 30);
const remainingDaysAftermonths = remainingDaysAfterYears - monthsLived * 30;
// calcule days
const daysLived = Math.floor(remainingDaysAftermonths);
// -----------if month and year are invalid
if (day > 31 || month > 12 || year > 2024) {
small.forEach((e) => {
e.style.display = "block";
return;
});
} else {
small.forEach((e) => {
e.style.display = "none";
yearsresult.innerHTML = `${yearsLived}`;
monthsresult.innerHTML = `${monthsLived}`;
daysresult.innerHTML = `${daysLived}`;
});
}
}
arrow.addEventListener("click", onClickArrow);