-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
51 lines (43 loc) · 1.37 KB
/
app.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
import getValuesObject from "./utils/getValueObject.js";
import validateInput from "./utils/validateInput.js";
import calculateAge from "./utils/calculateAge.js";
import updateCount from "./utils/updateCount.js";
// select button
const btn = document.querySelector(".btn");
// select inputs
const inputs = document.querySelectorAll("input");
// select spans
const valuePlaces = document.querySelectorAll("span");
// function to handle the click of the button
const handleClick = () => {
// Reset input values
inputs.forEach((input) => {
input.classList.remove("border-primaryLightRed");
input.previousElementSibling.classList.remove("text-primaryLightRed");
input.nextElementSibling.textContent = "";
});
// Get input values
const valuesObject = getValuesObject(inputs);
// validate input values
let hasError = validateInput(valuesObject, inputs);
if (!hasError) {
// Calculate age
const age = calculateAge(valuesObject, valuePlaces);
// updates the age values in the UI with animation
valuePlaces.forEach((item) => {
updateCount(item);
});
// Reset input values
inputs.forEach((input) => {
input.value = "";
});
}
};
// event listener on button
btn.addEventListener("click", handleClick);
// event listener on enter key
document.addEventListener("keydown", (e) => {
if (e.key === "Enter") {
btn.click();
}
});