-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
301 lines (245 loc) · 9.16 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
// buttons & divs
const operands = document.querySelectorAll('.operand')
const eqlbtn = document.querySelector('#calc-eql-btn')
const calcOutput = document.querySelector('#calc-output')
const exprDisplay = document.querySelector('#calc-output-expr')
const evalDisplay = document.querySelector('#calc-output-eval')
const clearBtn = document.querySelector('#calc-clear')
const backBtn = document.querySelector('#calc-back-btn')
const oneKey = document.querySelector('#one-key')
// event listeners
operands.forEach(operand => operand.addEventListener('click', main))
window.addEventListener('keydown', main)
eqlbtn.addEventListener('click', main)
backBtn.addEventListener('click', main)
clearBtn.addEventListener('mousedown', clearDisplay)
clearBtn.addEventListener('mousedown', () => {mouseDown = true});
clearBtn.addEventListener('mouseup', () => {mouseDown = false});
// operator objects & btns
function operatorCreator(expression, symbol, cssId) {
// Helper function for creating operator objects.
return {
expression,
symbol,
btn: document.querySelector(cssId)
}
}
const add = operatorCreator(...[function(x, y) {return x + y}, '+', '#calc-add-btn'])
const sub = operatorCreator(...[function(x, y) {return x - y}, '-', '#calc-sub-btn'])
const mult = operatorCreator(...[function(x, y) {return x * y}, 'x', '#calc-mult-btn'])
const div = operatorCreator(...[function(x, y) {return y === 0 ? NaN : (x / y)}, '÷', '#calc-div-btn'])
const exp = operatorCreator(...[function(x, y) {return x ** y}, '^', undefined])
add.btn.addEventListener('click', main)
sub.btn.addEventListener('click', main)
mult.btn.addEventListener('click', main)
div.btn.addEventListener('click', main)
// global variables
let operandOne = ''
let operator = undefined
let unconfirmedOperand = ''
let result = ''
let keylock = false;
const defaultMaxWidth = calcOutput.clientWidth
let mouseDown = false
function operate() {
// Evaluates the current expression.
if (operandOne && operator && unconfirmedOperand && !keylock) {
let value = operator.expression(Number(operandOne), Number(unconfirmedOperand))
if (isNaN(value)) {
keylock = true
value = 'hacker alert!'
} else if (value === Infinity)
keylock = true
return value.toString()
}
}
function isValidOperand() {
// Returns true if unconfirmedOperand is a valid number.
if (unconfirmedOperand) {
return !isNaN(Number(unconfirmedOperand))
}
}
function validateKeySelection(key) {
// Validates the user's key selection then updates the expression.
// determines if user input is an operator symbol
const operatorObj = [add, sub, mult, div].filter(oper => oper.symbol === key)[0]
// handles expression updates related to the expression's operator
if (operatorObj) {
updateOperator(operatorObj)
// handles expression updates related to an operand
} else {
updateOperand(key)
}
}
function updateOperand(key) {
// Validates the users key selection when updating the current operand
// pre-update validation checks
let canUpdate = true
// prevents the backspace character from being added to the operand
if (key === '←') {
unconfirmedOperand = unconfirmedOperand.slice(0, -1)
canUpdate = false
}
// prevents a second decimal point from being added to an operand
if (key === '.' && unconfirmedOperand.indexOf(key) !== -1) {
canUpdate = false
}
// prevents '=' from being added to the operand
if (key === '=') {
canUpdate = false
}
// prevents leading zeros in the operand
if (key === '0' && (!unconfirmedOperand || unconfirmedOperand.startsWith('-'))) {
canUpdate = false
}
if (canUpdate) {
unconfirmedOperand += key
}
}
function updateOperator(operatorObj) {
// Updates expression operator or positive/negative value of an operand.
// initially sets operandOne as a negative value
if (!operandOne && !unconfirmedOperand && operatorObj.symbol === '-') {
unconfirmedOperand = operatorObj.symbol
// initially sets operandOne as a positive value
} else if (!operandOne && unconfirmedOperand === '-' && operatorObj.symbol === '-') {
unconfirmedOperand = ''
}
// initially sets operator
if (!operator && isValidOperand()) {
operandOne = unconfirmedOperand
operator = operatorObj
unconfirmedOperand = ''
// updates operator before inputting a valid second operand
} else if (operandOne && !isValidOperand()) {
// simplifies expression by removing redundant operation symbols (e.g. 3 + -4 is the same as 3 - 4)
if (operator === sub && (operatorObj === sub || operatorObj == add)) {
operator = add
// sets the second operand as negative
} else if ((operator === mult || operator === div) && operatorObj === sub) {
unconfirmedOperand = operatorObj.symbol
// sets the operator as exponent
} else if (operator === mult && operatorObj === mult) {
operator = exp
} else {
operator = operatorObj
}
// updates the expression if an operator is entered as an expression evaluator
} else if (operandOne && operator && isValidOperand()) {
operandOne = result
unconfirmedOperand = ''
operator = operatorObj
if (operator.symbol === '÷') {
result = ''
}
}
}
function clearDisplay() {
// Removes last or all elements from expression based on length of time mouse button is held down.
setTimeout(() => {
// clear all expression elements if mouse button is held down
if (mouseDown) {
operandOne = ''
operator = undefined
unconfirmedOperand = ''
result = ''
// clear last expression element for normal mouse click
} else {
clearLastExpressionElement()
}
keylock = false
updateDisplay()
}
, 150)
}
function clearLastExpressionElement(e) {
// Removes last element from the curent expression.
if (result || unconfirmedOperand) {
result = ''
unconfirmedOperand = ''
} else {
operator = undefined
unconfirmedOperand = operandOne
operandOne = ''
}
keylock = false
updateDisplay()
}
function getFontSize(element) {
return Number(window.getComputedStyle(element).fontSize.slice(0, -2))
}
function adjustOutputFontSize(element, text) {
// Updates font size of text based upon scroll width of element.
const defaultFontSize = 48
// reduces font size while text is longer than calcOutput div
if (element.scrollWidth >= defaultMaxWidth) {
while (element.scrollWidth >= defaultMaxWidth) {
element.style.fontSize = (getFontSize(element) - .5) + 'px'
element.textContent = text
}
} else {
// increases font size while text is longer than calcOutput div
while (element.scrollWidth < defaultMaxWidth && getFontSize(element) < defaultFontSize) {
element.style.fontSize = (getFontSize(element) + .5) + 'px'
element.textContent = text
// reduces font size by one "step" if text exceeds the length of calcOutput div
if (element.scrollWidth >= defaultMaxWidth) {
element.style.fontSize = (getFontSize(element) - .5) + 'px'
break
}
}
}
}
function updateDisplay() {
// Updates the display panel of the calculator.
let expression = `${operandOne || unconfirmedOperand}${operator ? operator.symbol : ''}${operandOne ? unconfirmedOperand : ''}`
exprDisplay.textContent = expression
evalDisplay.textContent = result || null
adjustOutputFontSize(exprDisplay, exprDisplay.textContent)
if (result) {
adjustOutputFontSize(evalDisplay, evalDisplay.textContent)
}
}
function validateKeyboardInput(e) {
// Clears the calc display or returns the value of select keys.
let key
if (e.key === 'Delete') {
clearDisplay()
key = undefined
} else if (e.key === 'Enter') {
key = '='
} else if (e.key === 'Backspace') {
key = '←'
} else if (e.key === '*') {
key = 'x'
} else if (e.key === '/') {
e.preventDefault()
key = '÷'
} else if ('1234567890.+-'.indexOf(e.key) !== -1) {
key = e.key
} else {
key = undefined
}
return key
}
function main(e) {
// Validates user input then updates the expression and calculator display.
// normalizes keyboard or mouse input
let key
if (e.type === 'keydown') {
key = validateKeyboardInput(e)
} else {
key = e.target.textContent
}
if (!keylock && key) {
validateKeySelection(key)
// evaluates expression if second operand is a valid number & the operation will not cause an error
if (operandOne) {
if (operator.symbol !== '÷' && isValidOperand()) {
result = operate()
} else if (key === '=')
result = operate()
}
}
updateDisplay()
}