-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
182 lines (152 loc) · 4.89 KB
/
script.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Calculator Functions
const performCalculation = (num1, operation, num2) => {
const firstNumber = parseFloat(num1);
const secondNumber = parseFloat(num2);
if (operation === "divide" && secondNumber === 0) {
alert("You really shouldn't do that");
}
let result;
switch (operation) {
case "add":
result = firstNumber + secondNumber;
break;
case "subtract":
result = firstNumber - secondNumber;
break;
case "multiply":
result = firstNumber * secondNumber;
break;
case "divide":
result = firstNumber / secondNumber;
break;
default:
return NaN;
}
// Round the result to 4 decimal places if necessary
return Math.round(result * 10000) / 10000;
};
const determineKeyType = (key) => {
const actionType = key.dataset.action;
if (!actionType) return "number";
if (["add", "subtract", "multiply", "divide"].includes(actionType))
return "operator";
return actionType;
};
const generateResultDisplay = (key, currentDisplay, calculatorState) => {
const keyValue = key.textContent;
const keyType = determineKeyType(key);
const { firstValue, operator, modValue, previousKeyType } = calculatorState;
if (keyType === "number") {
return currentDisplay === "0" ||
previousKeyType === "operator" ||
previousKeyType === "calculate"
? keyValue
: currentDisplay + keyValue;
}
if (keyType === "decimal") {
if (!currentDisplay.includes(".")) return currentDisplay + ".";
if (previousKeyType === "operator" || previousKeyType === "calculate")
return "0.";
return currentDisplay;
}
if (keyType === "operator") {
return firstValue &&
operator &&
previousKeyType !== "operator" &&
previousKeyType !== "calculate"
? performCalculation(firstValue, operator, currentDisplay)
: currentDisplay;
}
if (keyType === "clear") return "0";
if (keyType === "delete") {
return currentDisplay === "0" ? "0" : currentDisplay.slice(0, -1);
}
if (keyType === "calculate") {
if (calculateClicked) {
return currentDisplay;
}
return firstValue
? previousKeyType === "calculate"
? performCalculation(currentDisplay, operator, modValue)
: performCalculation(firstValue, operator, currentDisplay)
: currentDisplay;
}
};
let calculateClicked = false;
const updateCalculatorState = (
key,
calculator,
calculatedValue,
currentDisplay
) => {
const keyType = determineKeyType(key);
const { firstValue, operator, modValue, previousKeyType } =
calculator.dataset;
calculator.dataset.previousKeyType = keyType;
if (keyType === "operator") {
calculator.dataset.operator = key.dataset.action;
calculator.dataset.firstValue =
firstValue &&
operator &&
previousKeyType !== "operator" &&
previousKeyType !== "calculate"
? calculatedValue
: currentDisplay;
calculateClicked = false;
}
if (keyType === "calculate") {
if (calculateClicked) {
return;
}
calculator.dataset.modValue =
firstValue && previousKeyType === "calculate" ? modValue : currentDisplay;
calculateClicked = true;
}
if (keyType === "clear" && key.textContent === "DEL") {
calculator.dataset.firstValue = "";
calculator.dataset.modValue = "";
calculator.dataset.operator = "";
calculator.dataset.previousKeyType = "";
calculateClicked = false;
}
};
const updateVisualAppearance = (key, calculator) => {
const keyType = determineKeyType(key);
const clearButton = calculator.querySelector("[data-action=clear]");
Array.from(key.parentNode.children).forEach((k) =>
k.classList.remove("is-depressed")
);
if (keyType === "operator") key.classList.add("is-depressed");
if (keyType === "clear" && key.textContent !== "DEL")
key.textContent = "RESET";
if (keyType !== "clear") clearButton.textContent = "RESET";
};
// Calculator Event Listener
const calculator = document.querySelector(".calculator-container");
const display = calculator.querySelector(".calculator-display");
const keys = calculator.querySelector(".calculator-keys");
keys.addEventListener("click", (event) => {
if (!event.target.matches("button")) return;
const key = event.target;
const currentDisplay = display.textContent;
const resultDisplay = generateResultDisplay(
key,
currentDisplay,
calculator.dataset
);
display.textContent = resultDisplay;
updateCalculatorState(key, calculator, resultDisplay, currentDisplay);
updateVisualAppearance(key, calculator);
});
// Theme Switcher
const themeToggler = document.querySelector(".theme-toggler");
const body = document.body;
themeToggler.addEventListener("click", () => {
if (body.classList.contains("dark")) {
body.classList.replace("dark", "light");
} else if (body.classList.contains("light")) {
body.classList.replace("light", "default");
} else {
body.classList.replace("default", "dark");
}
});