-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
164 lines (120 loc) · 4.48 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
document.addEventListener("DOMContentLoaded", () => {
var numberInput = [-1, 'x+', 20];
var numberOutput = [0, 'x+', -7];
var markedInput = Array(numberInput.length).fill(false); // Array to keep track of marked buttons
var markedOutput = Array(numberOutput.length).fill(false);
const buttonContainerInput = document.getElementById("button-container");
const buttonContainerOutput = document.getElementById("button-containerOutput");
const makeButton = document.getElementById("make");
const mI = document.getElementById("mI");
const tI = document.getElementById("tI");
const mO = document.getElementById("mO");
const tO = document.getElementById("tO");
const submitButton = document.getElementById("submit-button");
submitButton.addEventListener("click", () => {
numberInput[0] = parseFloat(mI.value);
numberInput[2] = parseFloat(tI.value);
numberOutput[0] = parseFloat(mO.value);
numberOutput[2] = parseFloat(tO.value);
console.log(numberInput);
updateButtonArray();
});
// Function to update the array of buttons
function updateButtonArray() {
buttonContainerInput.innerHTML = '';
// Add new dynamic buttons based on number1 values
for (let i = 0; i < numberInput.length; i++) {
const newButton = document.createElement("button");
newButton.className = "dynamic-button";
newButton.textContent = `${numberInput[i]}`;
// Set initial button color based on marked array
if (markedInput[i] && i != 1) {
newButton.style.backgroundColor = 'red';
} else {
newButton.style.backgroundColor = 'white';
}
newButton.addEventListener("click", () => {
// Toggle marked status and button color
markedInput[i] = !markedInput[i];
if (markedInput[i]) {
newButton.style.backgroundColor = 'red';
} else {
newButton.style.backgroundColor = 'white';
}
});
buttonContainerInput.appendChild(newButton);
}
buttonContainerOutput.innerHTML = '';
// Add new dynamic buttons based on number1 values
for (let i = 0; i < numberOutput.length; i++) {
const newButton = document.createElement("button");
newButton.className = "dynamic-button";
newButton.textContent = `${numberOutput[i]}`;
// Set initial button color based on marked array
if (markedOutput[i] && i != 1) {
newButton.style.backgroundColor = 'red';
} else {
newButton.style.backgroundColor = 'white';
}
newButton.addEventListener("click", () => {
// Toggle marked status and button color
markedOutput[i] = !markedOutput[i];
if (markedOutput[i]) {
newButton.style.backgroundColor = 'red';
} else {
newButton.style.backgroundColor = 'white';
}
});
buttonContainerOutput.appendChild(newButton);
}
}
function updateMake() {
makeButton.addEventListener("click", () => {
if (markedInput[2]) {
numberOutput[2] = parseFloat(numberOutput[2]) - parseFloat(numberInput[2]);
numberInput[2] = 0;
markedInput[2] = false;
markedOutput[2] = false;
}
if (markedOutput[2]) {
numberInput[2] = parseFloat(numberInput[2]) - parseFloat(numberOutput[2]);
numberOutput[2] = 0;
markedInput[2] = false;
markedOutput[2] = false;
}
if (markedInput[0] && parseFloat(numberInput[0]) != 0) {
numberOutput[0] = parseFloat(numberOutput[0]) / parseFloat(numberInput[0]);
numberOutput[2] = parseFloat(numberOutput[2]) / parseFloat(numberInput[0]);
numberInput[2] = parseFloat(numberInput[2]) / parseFloat(numberInput[0]);
numberInput[0] = parseFloat(numberInput[0]) / parseFloat(numberInput[0]);
markedInput[0] = false;
markedOutput[0] = false;
}
if (markedOutput[0] && parseFloat(numberOutput[0]) != 0) {
numberOutput[2] = parseFloat(numberOutput[2]) / parseFloat(numberOutput[0]);
numberInput[2] = parseFloat(numberInput[2]) / parseFloat(numberOutput[0]);
numberInput[0] = parseFloat(numberInput[0]) / parseFloat(numberOutput[0]);
numberOutput[0] = parseFloat(numberOutput[0]) / parseFloat(numberOutput[0]);
markedInput[0] = false;
markedOutput[0] = false;
}
if (markedInput[1]) {
numberOutput[0] = parseFloat(numberOutput[0]) - parseFloat(numberInput[0]);
numberInput[0] = 0;
markedInput[0] = false;
markedOutput[0] = false;
}
if (markedOutput[1]) {
numberInput[0] = parseFloat(numberInput[0]) - parseFloat(numberOutput[0]);
numberOutput[0] = 0;
markedInput[0] = false;
markedOutput[0] = false;
}
updateButtonArray();
}
)
}
// Initial call to populate the buttons
updateMake();
updateButtonArray();
});