-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
106 lines (92 loc) · 2.85 KB
/
Copy pathscript.js
File metadata and controls
106 lines (92 loc) · 2.85 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
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
const checkboxCard = document.querySelector(".checkbox-card");
const yearCards = document.querySelectorAll(".year-card");
const checkboxInputs = document.querySelectorAll(".checkbox-card input");
/* ------------------- FUNCTIONS ------------------- */
const displayElement = (element, show) => {
if (element) {
if (show) {
element.style.display = "flex";
} else {
element.style.display = "none";
}
}
};
const disableInputsExcept = (inputs, activeIndex) => {
for (let i = 0; i < 4; i++) {
if (activeIndex == i) {
continue;
}
inputs[i].disabled = true;
}
};
const enableAllInputs = (inputs) => {
for (let i = 0; i < 4; i++) {
inputs[i].disabled = false;
}
};
function randomInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
const reset = (inputs) => {
console.log(inputs);
for (let i = 0; i < 4; i++) {
inputs[i].value = 0;
inputs[i].disabled = false;
}
};
const calculate = (inputs, index, value) => {
const min = Number(value) - 100,
max = Number(value) + 100;
for (let i = 0; i < 4; i++) {
if (index == i) {
continue;
}
inputs[i].value = randomInteger(min, max);
}
enableAllInputs(inputs);
};
/* ------------------- MAIN LOGIC ------------------- */
/* 1. Year Cards Display */
checkboxCard.addEventListener("click", (e) => {
if (e.target.value) {
for (let i = 0; i < 3; i++) {
displayElement(yearCards[i], checkboxInputs[i].checked);
}
}
});
yearCards.forEach((yearCard) => {
const dropdown = yearCard.querySelector("select");
const monthsContainers = yearCard.querySelectorAll(".months-container"); // [Jan-Apr, May-Aug, Sep-Dec] : 3
/* 2. Month Inputs Display */
dropdown.onclick = () => {
for (let i = 0; i < 3; i++) {
if (i == dropdown.selectedIndex - 1) {
displayElement(monthsContainers[i], true);
} else {
displayElement(monthsContainers[i], false);
}
}
};
let displayedInputs, selectedIndex, indexValue;
/* 3. Disabling Other Inputs */
monthsContainers.forEach((container) => {
const inputs = container.querySelectorAll("input"); // [Jan, Feb, Mar, Apr]
inputs.forEach((input, index) => {
input.oninput = (e) => {
// Get info for reset and calculate
selectedIndex = index;
indexValue = e.target.value;
displayedInputs = inputs;
// Disable the other 3 inputs
disableInputsExcept(inputs, index);
};
});
});
/* 4. Reset */
const resetBtn = yearCard.querySelector(".reset-btn");
resetBtn.onclick = () => reset(displayedInputs);
/* 5. Calculate */
const calculateBtn = yearCard.querySelector(".calculate-btn");
calculateBtn.onclick = () =>
calculate(displayedInputs, selectedIndex, indexValue);
});