-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
executable file
·104 lines (86 loc) · 3.49 KB
/
main.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
import dayjs from "dayjs";
const submitHandel = async (e) => {
const [inpDay, inpMonth, inpYear] = document.querySelectorAll(".form__num__block__input");
const displayArr = document.querySelectorAll(".output__block__number");
e.preventDefault();
removeErrorsMessages();
if(validCheck(inpYear.value, inpMonth.value, inpDay.value)) return null;
const diffArr = calcAge(inpYear.value, inpMonth.value, inpDay.value);
for(let i = 0; i<displayArr.length; i++){
await animation(displayArr[i], diffArr[i]);
};
};
const animation = (displayLocation, endNumber) => {
const interval = 300;
let startNumber = 0;
const duration = Math.floor(interval / endNumber);
let counter = setInterval(() => {
displayLocation.textContent = startNumber;
if (startNumber === endNumber) clearInterval(counter);
startNumber++;
}, duration);
// wait for previous output to be counted before starting another one
return new Promise((resolve) => setTimeout(resolve, interval));
};
const calcAge = (birthYear, birthMonth, birthDate) => {
const birthFullDate = dayjs(`${birthYear}-${birthMonth}-${birthDate}`);
const today = dayjs();
const diffYear = today.diff(birthFullDate, "y");
const diffMonth = today.diff(birthFullDate, "M") - 12 * diffYear;
// x is the value that is used to count number backwards when birthday is after today eg going from 15 to 30 to again 5
const x = birthDate <= today.date() ? 0 : today.daysInMonth();
const diffDate = x + today.date() - birthDate;
return [diffYear, diffMonth, diffDate];
};
const validCheck = (birthYear, birthMonth, birthDate) => {
const inpBoxArr = document.querySelectorAll(".form__num__block__input");
const labelArr = document.querySelectorAll(".form__num__block__label");
const errorArr = document.querySelectorAll(".form__num__block__error");
const [errorDay, errorMonth, errorYear] = errorArr;
const displayArr = document.querySelectorAll(".output__block__number");
const birthFullDate = dayjs(`${birthYear}-${birthMonth}-${birthDate}`);
let wrong = false;
const dateArr = [birthDate, birthMonth, birthYear];
for(let i = 0; i<dateArr.length; i++){
// empty input field
if(dateArr[i] === "") {
errorArr[i].textContent = "This field is required";
wrong = true;
};
};
console.log();
if (birthDate > dayjs(`${birthYear}-${birthMonth}`).daysInMonth() || birthDate === "0") {
// day doesn't exist
errorDay.textContent = "Must be a valid day";
wrong = true;
}
if (birthMonth > 12 || birthMonth === "0"){
// month doesn't exist
errorMonth.textContent = "Must be a valid month";
wrong = true;
};
if(dayjs().diff(birthFullDate)<0){
// day in the future
errorYear.textContent = "Must be in the past";
wrong = true;
};
if(wrong){
for(let i = 0; i < inpBoxArr.length; i++){
inpBoxArr[i].style.borderColor = "var(--light-red)";
labelArr[i].style.color = "var(--light-red)";
displayArr[i].textContent = "--";
};
};
return wrong;
};
const removeErrorsMessages = () => {
const inpBoxArr = document.querySelectorAll(".form__num__block__input");
const labelArr = document.querySelectorAll(".form__num__block__label");
const errorArr = document.querySelectorAll(".form__num__block__error");
for(let i = 0; i<inpBoxArr.length; i++) {
inpBoxArr[i].style.borderColor = "var(--light-grey)";
labelArr[i].style.color = "var(--smokey-grey)";
errorArr[i].textContent = "";
};
};
document.querySelector("form").addEventListener("submit", submitHandel);