-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
190 lines (171 loc) · 5.89 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
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
183
184
185
186
187
188
189
190
const numberButtons = document.querySelectorAll(".number-button");
const insertPlace = document.querySelector("#output");
let currentNumber = "";
let firstNumber = null;
let operation = null;
let result = null;
function handleNumberInput(number) {
const newSpan = document.createElement("span");
if (number === ",") {
currentNumber += ".";
newSpan.innerText = ".";
} else {
currentNumber += number;
newSpan.innerText = number;
}
insertPlace.appendChild(newSpan);
console.log("Pressed Number is: ", number);
}
numberButtons.forEach((button) => {
button.addEventListener("click", () => {
const number = button.innerText;
handleNumberInput(number);
});
});
window.addEventListener("keydown", (event) => {
const key = event.key;
const number = parseInt(key);
if (!isNaN(number)) {
handleNumberInput(number.toString());
} else if (key === "+") {
handleOperationButton.bind(addButton, "add")();
} else if (key === "-") {
handleOperationButton.bind(subtractButton, "subtract")();
} else if (key === "*") {
handleOperationButton.bind(multiplyButton, "multiply")();
} else if (key === "/") {
handleOperationButton.bind(divideButton, "divide")();
}
});
const addButton = document.getElementById("add");
const subtractButton = document.getElementById("subtract");
const multiplyButton = document.getElementById("multiply");
const divideButton = document.getElementById("divide");
const calculateButton = document.getElementById("calculate");
const clearButton = document.getElementById("clear");
function handleOperationButton(operationType) {
if (firstNumber === null) {
firstNumber = parseFloat(currentNumber);
currentNumber = "";
insertPlace.innerText = "";
} else if (currentNumber !== "") {
performCalculation();
currentNumber = "";
insertPlace.innerText = "";
}
insertPlace.innerText = "";
operation = operationType;
}
addButton.addEventListener("click", () => handleOperationButton("add"));
subtractButton.addEventListener("click", () =>
handleOperationButton("subtract")
);
multiplyButton.addEventListener("click", () =>
handleOperationButton("multiply")
);
divideButton.addEventListener("click", () => handleOperationButton("divide"));
calculateButton.addEventListener("click", () => {
if (firstNumber !== null && currentNumber !== "") {
performCalculation();
}
});
clearButton.addEventListener("click", () => {
firstNumber = null;
currentNumber = "";
insertPlace.innerText = "";
});
function performCalculation() {
const secondNumber = parseFloat(currentNumber);
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;
}
console.log("Result: ", result);
firstNumber = result;
currentNumber = "";
insertPlace.innerText = result;
}
//Dark Mode
let isDark = false
function darkMode(){
if(isDark == false){
document.body.classList.add("bg-zinc-900");
document.querySelectorAll(".textToWhite").forEach(e =>
e.classList.add("text-white")
)
document.querySelectorAll(".borderToWhite").forEach(e =>
e.classList.add("border-white")
)
//DarkMode button
document.querySelector(".darkModeBtn").classList.add("bg-zinc-900")
document.querySelector(".darkModeBtn").classList.add("hover:bg-white")
document.querySelector(".darkModeBtn").classList.add("hover:text-black")
//Calculator Buttons
document.querySelector(".bg-slate-600").classList.add("bg-neutral-900")
document.querySelector(".bg-slate-600").classList.remove("bg-slate-600")
document.querySelectorAll(".bg-slate-500").forEach(e => {
e.classList.add("bg-neutral-800")
e.classList.remove("bg-slate-500")
e.classList.add("hover:bg-neutral-900")
e.classList.remove("hover:bg-slate-600")
})
document.querySelectorAll(".bg-slate-400").forEach(e => {
e.classList.add("bg-neutral-700")
e.classList.remove("bg-slate-400")
e.classList.add("hover:bg-neutral-800")
e.classList.remove("hover:bg-slate-500")
})
document.querySelectorAll(".bg-orange-500").forEach(e => {
e.classList.add("bg-orange-800")
e.classList.remove("bg-orange-500")
e.classList.add("hover:bg-orange-900")
e.classList.remove("hover:bg-orange-600")
})
isDark = true
}
else{
document.body.classList.remove("bg-zinc-900");
document.querySelectorAll(".textToWhite").forEach(e =>
e.classList.remove("text-white")
)
document.querySelectorAll(".borderToWhite").forEach(e =>
e.classList.remove("border-white")
)
//DarkMode button
document.querySelector(".darkModeBtn").classList.remove("bg-zinc-900")
document.querySelector(".darkModeBtn").classList.remove("hover:bg-white")
document.querySelector(".darkModeBtn").classList.remove("hover:text-black")
//Calculator Buttons
document.querySelector(".bg-neutral-900").classList.add("bg-slate-600")
document.querySelector(".bg-neutral-900").classList.remove("bg-neutral-900")
document.querySelectorAll(".bg-neutral-800").forEach(e => {
e.classList.remove("bg-neutral-800")
e.classList.add("bg-slate-500")
e.classList.remove("hover:bg-neutral-900")
e.classList.add("hover:bg-slate-600")
})
document.querySelectorAll(".bg-neutral-700").forEach(e => {
e.classList.remove("bg-neutral-700")
e.classList.add("bg-slate-400")
e.classList.remove("hover:bg-neutral-800")
e.classList.add("hover:bg-slate-500")
})
document.querySelectorAll(".bg-orange-800").forEach(e => {
e.classList.remove("bg-orange-800")
e.classList.add("bg-orange-500")
e.classList.remove("hover:bg-orange-900")
e.classList.add("hover:bg-orange-600")
})
isDark = false
}
}