-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
263 lines (234 loc) · 6.9 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
//FUNCTIONS
// add
function add(a, b) {
return a + b;
}
// subtract
function subtract(a, b) {
return a - b;
}
// multiply
function multiply(a, b) {
return a * b;
}
// divide
function divide(a, b) {
return a / b;
}
//takes in input from button. tracks all numbers and operations in an array
let calcArray = [""];
function takeInput(buttonInput, calcArray) {
playRandSound();
//Take Backspace as input
if (buttonInput === "backspace") {
calcArray[calcArray.length - 1] = calcArray[calcArray.length - 1].slice(
0,
-1
);
return;
}
if (typeof buttonInput === "function") {
//Replace last index if last index isn't a number (meaning it's a function or '' empty string)
if (!!isNaN(parseFloat(calcArray.slice(-1)))) {
calcArray[calcArray.length - 2] = buttonInput;
} else {
calcArray.push(buttonInput);
calcArray.push("");
}
} else if (typeof +buttonInput === "number") {
//Check if the string contains decimal
if (buttonInput === "." && calcArray[calcArray.length - 1].includes(".")) {
return;
} else {
calcArray[calcArray.length - 1] += buttonInput;
}
}
return;
// }}
}
//Variables for Operation & Numbers
/** @param {Array} calcArray */
function execOperation(calcArray) {
//TODO: potential error handling
//basically my own version of the reduce method
//perform operation for the first 3 elements of calcArray
// operation(element1, element2)
let result = calcArray[1](+calcArray[0], +calcArray[2]);
//result becomes first element of array
if (!isFinite(result)) return false;
if (result % 1 != 0) result = parseFloat(result.toFixed(6));
calcArray[2] = result;
calcArray.splice(0, 2);
amongUsSound()
return true;
}
/** @param {Array} calcArray */
function updateDisplay(calcArray) {
let operatorSymbol;
//swap all the functions to displayable symbols
const calcArraySymbols = calcArray.map((val, index, array) => {
if (typeof val === "function") {
switch (val.name) {
case "add":
operatorSymbol = "+";
break;
case "subtract":
operatorSymbol = "-";
break;
case "multiply":
operatorSymbol = "×";
break;
case "divide":
operatorSymbol = "÷";
break;
}
return operatorSymbol;
}
return val;
});
if (calcArray[0] === "") {
document.querySelector(".display#main").innerHTML = " ";
} else {
const calcArrayString = calcArraySymbols.join(" ");
document.querySelector(".display#main").textContent = calcArrayString;
}
}
function clearDisplay() {
calcArray = [""];
updateDisplay(calcArray);
const audio = document.querySelector("#complete")
audio.play()
}
function addTransition() {
this.classList.add("pressed");
}
function removeTransition(e) {
// console.log(e);
if (e.propertyName !== "transform") return;
this.classList.remove("pressed");
}
function playRandSound() {
const randNum = Math.ceil(Math.random() * 4);
const sound = document.querySelector(`.sound#a${randNum}`);
sound.currentTime = 0;
sound.play();
}
function amongUsSound(){
const sound = document.querySelector(`.sound#amongus`);
sound.currentTime = 0;
sound.play();
}
//Button Event Listeners
document.querySelector("button#ac").addEventListener("click", clearDisplay);
//FIXME:ONE function that checks valid button clicks
//= Buttons
//Decimal Button
document.querySelector("button#decimal").addEventListener("click", (e) => {
takeInput(".", calcArray);
updateDisplay(calcArray);
});
//Number buttons
const numberKeys = document.querySelectorAll(".number");
numberKeys.forEach((numberKey) => {
numberKey.addEventListener("click", (e) => {
takeInput(e.target.id.slice(1), calcArray);
updateDisplay(calcArray);
// numberKey.classList.add("pressed")
});
});
//Equals button
document.querySelector("button#equals").addEventListener("click", () => {
if (execOperation(calcArray)) {
updateDisplay(calcArray);
} else {
updateDisplay(calcArray);
document.querySelector(".display#main").textContent = "invalid result";
}
});
document.querySelector("#add").addEventListener("click", (e) => {
takeInput(add, calcArray);
});
document.querySelector("#subtract").addEventListener("click", (e) => {
takeInput(subtract, calcArray);
});
document.querySelector("#multiply").addEventListener("click", (e) => {
takeInput(multiply, calcArray);
});
document.querySelector("#divide").addEventListener("click", (e) => {
takeInput(divide, calcArray);
});
//Backspace
document.querySelector("#backspace").addEventListener("click", (e) => {
takeInput("backspace", calcArray);
});
//Operations Eventlistener
document.querySelectorAll("button.operator").forEach((btn) =>
btn.addEventListener("click", () => {
updateDisplay(calcArray);
})
);
const allKeys = document.querySelectorAll("button");
//Keyboard support
window.addEventListener("keydown", (e) => {
//Check if key is a supported number
if (Array.from(numberKeys).some((item) => item.innerHTML === e.key)) {
document.querySelector(`#n${e.key}`).classList.add("pressed");
takeInput(e.key, calcArray);
updateDisplay(calcArray);
} else {
//Check check other keys
switch (e.key) {
case ".":
takeInput(e.key, calcArray);
document.querySelector(`#decimal`).classList.add("pressed");
break;
case "+":
takeInput(add, calcArray);
document.querySelector(`#add`).classList.add("pressed");
break;
case "-":
takeInput(subtract, calcArray);
document.querySelector(`#subtract`).classList.add("pressed");
break;
case "=":
case "Enter":
document.querySelector(`#equals`).classList.add("pressed");
playRandSound();
if (execOperation(calcArray)) {
updateDisplay(calcArray);
} else {
updateDisplay(calcArray);
document.querySelector(".display#main").textContent =
"invalid result";
}
break;
case "/":
takeInput(divide, calcArray);
document.querySelector(`#divide`).classList.add("pressed");
break;
case "Backspace":
takeInput("backspace", calcArray);
document.querySelector(`#backspace`).classList.add("pressed");
break;
case "x":
case "X":
case "*":
takeInput(multiply, calcArray);
document.querySelector(`#multiply`).classList.add("pressed");
break;
case "Escape":
document.querySelector(`#ac`).classList.add("pressed");
playRandSound();
clearDisplay();
break;
}
updateDisplay(calcArray);
}
});
allKeys.forEach((key) => {
key.addEventListener("click", addTransition);
key.addEventListener("click", playRandSound);
key.addEventListener("transitionend", removeTransition);
});
const uwu = document.querySelector('#uwu')
document.querySelector('.heart').addEventListener('click', ()=> uwu.play())