-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
372 lines (287 loc) · 12.4 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// SELECTORS
const display = document.getElementById('display') ;
const expressionDisplay = document.getElementById('top-display') ;
const clearBtn = document.getElementById('clear-btn') ;
const deleteBtn = document.getElementById('delete-btn') ;
const posNegBtn = document.getElementById('positive-negative') ;
const decimalBtn = document.getElementById('decimal-btn') ;
const equalsBtn = document.getElementById('equals-btn') ;
const numBtns = document.getElementsByClassName('num') ;
const operatorBtns = document.getElementsByClassName('operator') ;
// VARIABLES
let firstInput = null // Stores our first operand
let secondInput = null // Stores our second operand
let displayValue = ''; // Stores the user's number inputs as a string
// Boolean Logic:
let activeOperator = null // Tells us what operator is currently active
let displayToBeCleared = false // Tells us if the display needs to be cleared/reset on next user input
let canDelete = true // Tells us if we can use the delete/backspace function on our displays
let canOperate = true // Tells us if user can input another operator
let activeDecimal = false // Tells us if a decimal is being used
// OPERATOR FUNCTIONS
function add(a, b){
let answer = a + b
firstInput = answer
secondInput = null
displayValue = ''
activeOperator = null
displayToBeCleared = true
canDelete = false
activeDecimal = false
if (answer % 1 !== 0){ // If answer has a decimal point in it --> round to 3rd decimal place
answer = answer.toFixed(3)
}
if (answer % 1 === 0) { // If no decimal point --> round to be a whole number
answer = answer.toFixed(0)
}
display.innerText = answer.toString()
expressionDisplay.innerText = answer.toString()
}
function subtract(a, b){
let answer = a - b
firstInput = answer
secondInput = null
displayValue = ''
activeOperator = null
displayToBeCleared = true
canDelete = false
activeDecimal = false
if (answer % 1 !== 0){ // If answer has a decimal point in it --> round to 3rd decimal place
answer = answer.toFixed(3)
}
if (answer % 1 === 0) { // If no decimal point --> round to be a whole number
answer = answer.toFixed(0)
}
display.innerText = answer.toString()
expressionDisplay.innerText = answer.toString()
}
function multiply(a, b){
let answer = a * b
firstInput = answer
secondInput = null
displayValue = ''
activeOperator = null
displayToBeCleared = true
canDelete = false
activeDecimal = false
if (answer % 1 !== 0){ // If answer has a decimal point in it --> round to 3rd decimal place
answer = answer.toFixed(3)
}
if (answer % 1 === 0) { // If no decimal point --> round to be a whole number
answer = answer.toFixed(0)
}
display.innerText = answer.toString()
expressionDisplay.innerText = answer.toString()
}
function division(a, b){
if (b == 0){
setTimeout(clearExpression, 1)
setTimeout(clear, 1)
alert("Nice try, you can't divide by zero! How about you try that again?")
clear()
}
else {
let answer = a / b
firstInput = answer
secondInput = null
activeOperator = null
displayValue = ''
displayToBeCleared = true
canDelete = false
activeDecimal = false
if (answer % 1 !== 0){ // If answer has a decimal point in it --> round to 3rd decimal place
answer = answer.toFixed(3)
}
if (answer % 1 === 0) { // If no decimal point --> round to be a whole number
answer = answer.toFixed(0)
}
display.innerText = answer.toString()
expressionDisplay.innerText = answer.toString()
}
}
function operate(){ // Calculator's Logic to determine what operator function to apply
if (activeOperator === 'plus-btn') return add(firstInput, secondInput)
if (activeOperator === 'minus-btn') return subtract(firstInput, secondInput)
if (activeOperator === 'times-btn') return multiply(firstInput, secondInput)
if (activeOperator === 'divide-btn') return division(firstInput, secondInput)
}
// DISPLAY & INPUT FUNCTIONS
function displayUserInput(e) { // Shows user's number input on calculator display & saves it to displayValue
if (firstInput && !activeOperator) return
else if (displayToBeCleared === true) {
clearDisplay()
displayToBeCleared = false
canDelete = true
}
displayValue += e.target.innerText
display.innerText += `${e.target.innerText}`
}
function clearDisplay() { // Clears saved user number input (displayValue) & the calculator's display
displayValue = '';
display.innerText = '' ;
posNegInUse = false
}
function clearExpression() { // Clears the expression display
expressionDisplay.innerText = ''
}
function removeLastDisplay(){ // Removes/deletes the last value entered by user (from the calculator display & from displayValue)
display.innerText = display.innerText.substring(0, display.innerHTML.length-1) ;
displayValue = displayValue.substring(0, displayValue.length-1) ;
}
function removeLastExpression() { // Removes/deletes last number currently in the expression display
if (canDelete === true && display.innerText !== "" ) {
expressionDisplay.innerText = expressionDisplay.innerText.substring(0, expressionDisplay.innerText.length-1)
removeLastDisplay()
} else {
return
}
}
function clear() { // Clears all information stored & resets the caluclator to it's original state
firstInput = null
secondInput = null
activeOperator = null
displayToBeCleared = false
canDelete = true
expressionDisplay.innerText = ''
activeDecimal = false
clearDisplay()
}
function displayExpression(e) { // Shows user's operator & number inputs as an expression in the expression display
if (firstInput && !activeOperator) return
else expressionDisplay.innerText += `${e.target.innerText}`
}
// LOGIC FUNCTIONS
function evaluateOperator(e) { // Evaluates if & what operator is clicked/active & whether a calculation should be done
if (activeOperator === null) {
if (display.innerText !== "0" && display.innerText === ''){ // Don't allow user to enter an operator & reset calculator
setTimeout(clearExpression, 1)
setTimeout(clear, 1)
return alert('ERROR: You need to enter a number before you can enter an operator! Please input again.')
} else { // Allow user to enter an operator, apply logic, & operate on it
activeOperator = e.target.id
if (firstInput === null) firstInput = Number(displayValue)
activeDecimal = false
clearDisplay()
canDelete = true
}
}
else if (activeOperator !== null) {
if ((!displayValue && !displayValue.length) ) { // Don't allow user to enter an operator & reset calculator
setTimeout(clearExpression, 1)
setTimeout(clear, 1)
return alert('ERROR: Entered in an operator twice! Please input again.')
} else { // Allow user to enter an operator, apply logic, & operate on it
secondInput = Number(displayValue)
canDelete = false
clearDisplay()
operate(activeOperator)
activeOperator = e.target.id
}
}
}
function evaluateDecimal(e) { // Evaluates if our user has already entered a decimal
if (!display.innerText) { // Allow user to input a decimal, display it & apply logic
displayUserInput(e)
displayExpression(e)
activeDecimal = true
}
else if (display.innerText) {
if (activeDecimal === false) {
activeDecimal = true
displayUserInput(e)
displayExpression(e)
}
else if (activeDecimal === true) return // Don't allow a decimal to be inputted by user & do nothing
}
}
function evaluateEquals() { // Evaluates if current input(s)/expression can be operated on when '=' is clicked
if ( (firstInput || firstInput === 0) && displayValue ) { // Allow user to input '=', apply logic, & operate on it.
secondInput = Number(displayValue)
canDelete = false
clearDisplay()
operate(activeOperator)
}
else if ( (firstInput && !displayValue)
|| (!firstInput)
|| (!activeOperator) ) { // Don't allow user to input an '=' & reset calculator
return
}
}
function evaluatePosNeg() { // Converts user's current input from positive to negative & vice versa
if (!displayValue && firstInput && !activeOperator) { // The state after user hits '=' button & a calculation is made
if (firstInput >= 0) {
firstInput = firstInput * -1
display.innerText = firstInput.toString()
expressionDisplay.innerText = `${firstInput.toString()}`
} else if (firstInput < 0) {
firstInput = firstInput * -1
display.innerText = firstInput.toString()
expressionDisplay.innerText = firstInput.toString()
}
}
else if (displayValue && !firstInput ) { // The starting state of the calculator
if (Number(displayValue) >= 0){
displayValue = (Number(displayValue) * -1).toString()
display.innerText = displayValue
expressionDisplay.innerText = expressionDisplay.innerText.substring(0, expressionDisplay.innerText.length - (displayValue.length -1) )
expressionDisplay.innerText = `${displayValue}`
} else if (Number(displayValue) < 0) {
displayValue = (Number(displayValue) * -1).toString()
display.innerText = displayValue
expressionDisplay.innerText = expressionDisplay.innerText.substring(0, expressionDisplay.innerText.length - (displayValue.length -1) )
expressionDisplay.innerText = `${displayValue}`
}
}
else if (displayValue && firstInput && activeOperator) { // The state of the caluclator after user enters a first input & an operator
if (Number(displayValue) >= 0) {
displayValue = (Number(displayValue) * -1).toString()
display.innerText = displayValue
let operatorIndex = expressionDisplay.innerText.indexOf(operatorStr()) // Index of the operator symbol in the expression display
let currentDisplayInput = expressionDisplay.innerText.substring(operatorIndex + 1)
let firstInputAndOperator = expressionDisplay.innerText.substring(0, operatorIndex + 1)
expressionDisplay.innerText = `${firstInputAndOperator}(-${currentDisplayInput})`
} else if (Number(displayValue) < 0) {
displayValue = (Number(displayValue) * -1).toString()
display.innerText = displayValue
let operatorIndex = expressionDisplay.innerText.indexOf(operatorStr()) // Index of the operator symbol in the expression display
let firstInputAndOperator = expressionDisplay.innerText.substring(0, operatorIndex + 1)
expressionDisplay.innerText = `${firstInputAndOperator}${displayValue}`
}
}
}
function operatorStr() { // Converts our currently 'activeOperator' to it's corresponding string
if (activeOperator === 'plus-btn') return '+'
if (activeOperator === 'minus-btn') return '-'
if (activeOperator === 'times-btn') return '×'
if (activeOperator === 'divide-btn') return '÷'
}
// CLICK EVENTS
for (const buttons of numBtns) {
buttons.addEventListener('click', displayUserInput )
}
for (const buttons of operatorBtns) {
buttons.addEventListener('click', evaluateOperator)
}
for (const buttons of numBtns) {
buttons.addEventListener('click', displayExpression)
}
for (const buttons of operatorBtns) {
buttons.addEventListener('click', displayExpression)
}
clearBtn.addEventListener('click', clear)
deleteBtn.addEventListener('click', removeLastExpression)
equalsBtn.addEventListener('click', evaluateEquals)
decimalBtn.addEventListener('click', evaluateDecimal)
posNegBtn.addEventListener('click', evaluatePosNeg)
// KEYDOWN EVENTS
document.addEventListener('keydown', e => {
if (e.key === 'Backspace' || e.key.toLowerCase() === 'd'){
removeLastExpression()
}
if ((e.key === 'Backspace' && e.shiftKey === true) || e.key.toLowerCase() === 'c' ) {
clear()
}
if (e.key.toLowerCase() === 'p' || e.key.toLowerCase() === 'n') {
evaluatePosNeg()
}
})