-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
254 lines (226 loc) · 6.48 KB
/
script.js
File metadata and controls
254 lines (226 loc) · 6.48 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
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
// Shortcut to assign grid-areas to each button
let btns = document.querySelectorAll(".btn");
btns.forEach(btn => btn.style.cssText = `grid-area: ${btn.id};`);
// Initilaize default display values
let userInput = 0;
let memory = 0;
let operator = '~';
let result = 0
// Used for tracking decimal places
let place = 0;
// Toggle for new userInput overwrite, used after an operator is clicked
let freezeInput = false;
// Toggle for whether there is a decimal in the userInput
let isFloat = false;
// Grab the 4 displays to play with later
const inputDisplay = document.querySelector("#input-display");
const operatorDisplay = document.querySelector(".operation");
const memoryDisplay = document.querySelector(".memory");
const resultDisplay = document.querySelector(".result");
// Grab all the different buttons to play with
const operators = [...document.getElementsByClassName("operator")];
const numbers = [...document.getElementsByClassName("number")];
const clear = document.getElementById("clear");
const backspace = document.getElementById("backspace");
const equals = document.getElementById("equals");
const decimal = document.getElementById("decimal");
// Add event listeners to each category of button
operators.forEach(operator => {
operator.addEventListener("click", (e) => updateOperator(e));
});
// KeyboardEvent.key Value === "*", "?", "-", "+"
numbers.forEach(number => {
number.addEventListener("click", (e) => updateInput(e));
});
// KeyboardEvent.key Value === "0" - "9"
clear.addEventListener("click", () => clearAll());
// KeyboardEvent.key Value === "Backspace"
backspace.addEventListener("click", () => backspaceChar());
// KeyboardEvent.key Value === "Enter"
equals.addEventListener("click", () => updateResult());
// KeyboardEvent.key Value "Decimal"
decimal.addEventListener("click", () => makeFloat());
// Add event listeners to key-down events with specific keyIds
document.addEventListener("keydown", function(e) {
if (isFinite(parseInt(e.key))) {
keyUpdateInput(e);
} else if (e.key === "+" || e.keyValue === "-" || e.keyValue === "/" || e.keyValue === "*") {
keyUpdateOperator(e);
} else if (e.key === "Backspace") {
backspaceChar();
} else if (e.key === "Enter") {
updateResult();
} else if (e.key === ".") {
makeFloat();
} else {
return;
}
})
// Shows the number the user has entered
function keyUpdateInput(e) {
// Check if the last button clicked was an operator
if (freezeInput === true) {
userInput = 0;
freezeInput = false;
}
// If it isn't a float, proceed normally
if (!isFloat) {
if (userInput === 0) {
userInput += parseInt(e.key);
} else {
userInput = (userInput * 10) + parseInt(e.key);
}
}
// If it is a float, user has clicked the decimal
if (isFloat) {
place ++;
userInput += (parseInt(e.key) / 10 ** place);
userInput = Math.round(userInput * (10 ** place)) / (10 ** place);
}
updateInputDisplay();
};
// Shows the number the user has entered
function updateInput(e) {
// Check if the last button clicked was an operator
if (freezeInput === true) {
userInput = 0;
freezeInput = false;
}
// If it isn't a float, proceed normally
if (!isFloat) {
if (userInput === 0) {
userInput += parseInt(e.target.innerText);
} else {
userInput = (userInput * 10) + parseInt(e.target.innerText);
}
}
// If it is a float, user has clicked the decimal
if (isFloat) {
place ++;
userInput += (parseInt(e.target.innerText) / 10 ** place);
userInput = Math.round(userInput * (10 ** place)) / (10 ** place);
}
updateInputDisplay();
};
function updateInputDisplay() {
inputDisplay.innerText = `${userInput}`;
};
function updateResult() {
// Calculates!
result = operate(operator, memory, userInput);
// Update the memory and displays only if the values are not undefined (from dividing by 0)
if (memory && result) {
updateMemoryDisplay();
memory = result;
updateResultDisplay();
}
if (!result) {
result = 0;
}
};
function updateResultDisplay() {
resultDisplay.innerText = `${result}`;
};
// Updates the displayed operator and memory/result
function keyUpdateOperator(e) {
place = 0;
isFloat = false;
// Gets a result if there is already an operator
if (operator !== '~') {
updateResult();
} else {
updateMemory();
}
operator = e.key;
updateOperatorDisplay();
// Allows the user to keep hitting operators without clearing their userInput
freezeInput = true;
};
// Updates the displayed operator and memory/result
function updateOperator(e) {
place = 0;
isFloat = false;
// Gets a result if there is already an operator
if (operator !== '~') {
updateResult();
} else {
updateMemory();
}
operator = e.target.innerText;
updateOperatorDisplay();
// This allows the user to keep hitting operators without clearing their userInput
freezeInput = true;
};
function updateOperatorDisplay() {
operatorDisplay.innerText = `${operator}`;
};
// Move userInput to Memory
function updateMemory() {
updateMemoryDisplay();
memory = userInput;
};
function updateMemoryDisplay() {
memoryDisplay.innerText = `${memory} ${operator} ${userInput}`;
};
function makeFloat() {
if (!isFloat) {
isFloat = true;
};
}
// Update all displays
function updateDisplays() {
updateMemoryDisplay();
updateInputDisplay();
updateOperatorDisplay();
updateResultDisplay();
};
// Restore all values to default and updates displays
function clearAll() {
userInput = 0;
memory = 0;
operator = '~';
result = 0;
place = 0;
isFloat = false;
updateDisplays();
};
function backspaceChar() {
if (!isFloat) {
userInput = Math.floor(userInput / (10));
} else {
let inputString = userInput.toString();
let len = inputString.length;
userInput = parseFloat(inputString.slice(0, len - 1));
}
if (place > 0) {
place--;
}
if (place === 0) {
isFloat = false;
}
updateInputDisplay();
};
function add (numA, numB) {return numA + numB};
function subtract (numA, numB) {return numA - numB};
function multiply (numA, numB) {return numA * numB};
function divide (numA, numB) {
if (numB === 0) {
alert("YOU SHALLL NOT PASSSSSSSSS!");
return;
} else {
return numA / numB
}
};
function operate(operator, numA, numB) {
if(operator == "+") {
return add(numA, numB);
} else if (operator == "-") {
return subtract(numA, numB);
} else if (operator == "*") {
return multiply(numA, numB);
} else if (operator == "/") {
return divide(numA, numB);
} else{
return '~';
}
};