-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
268 lines (201 loc) · 6.36 KB
/
script.js
File metadata and controls
268 lines (201 loc) · 6.36 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//CALCULATIONS
function add(a, b) {
return a + b;
}
function subtract(a, b) {
return a - b;
}
function multiply(a, b) {
return a * b;
}
function divide(a, b) {
if(b === 0) {
alert('Dividing by zero is forbidden.');
clear();
return;
}
return a / b;
}
function calculate(a, operation, b) {
const aFloat = parseFloat(a);
const bFloat = parseFloat(b);
let solution;
switch (operation) {
case '+':
solution = add(aFloat, bFloat);
break;
case '-':
solution = subtract(aFloat, bFloat);
break;
case 'x':
solution = multiply(aFloat, bFloat);
break;
case '/':
solution = divide(aFloat, bFloat);
break;
}
operator = '';
return solution.toString().length > 11 ? (Math.round((solution + Number.EPSILON) * 100000000) / 100000000) : solution;
}
//LOGIC FOR INPUT HANDLING AND OPERATION
//variables
let displayValue = '';
let operator;
let lastOperator;
let firstValue;
let secondValue;
let firstValueSet = false; //checks if the value may be overwritten
const inputField = document.querySelector('.input-field');
const inputButtons = document.querySelectorAll('.input-buttons');
const operatorButtons = document.querySelectorAll('.grid-operator-buttons button');
const equalButton = document.getElementById('equal');
const clearButton = document.getElementById('clear');
const backspaceButton = document.getElementById('backspace');
const negateButton = document.getElementById('negate');
//functions
function populate(input) {
if (key.textContent === '.' && displayValue.includes('.')) return; //prevents more than one '.' per value
displayValue += input;
inputField.textContent = displayValue;
}
function setValue(input) { //Set values, depending on which value is already set
if (!firstValueSet) {
firstValue = input;
firstValueSet = true;
} else {
secondValue = input;
};
displayValue = '';
}
function negateInputValue() {
if (displayValue === '') return;
!displayValue.includes('-') ? displayValue = `-${displayValue}` : displayValue = displayValue.slice(1);
inputField.textContent = displayValue;
}
function clear() {
displayValue = '';
firstValue = '';
firstValueSet = false;
secondValue = '';
populate('');
toggleClickedClass();
inputButtons.forEach(button => button.classList.remove('active'));
}
function backspace() {
if (displayValue === '') return; //prevents deletion of solutions
displayValue = inputField.textContent.slice(0,inputField.textContent.length-1)
inputField.textContent = displayValue;
}
function toggleClickedClass() { //removes the 'operator-buttons-clicked' class if other operator button is clicked
operatorButtons.forEach(button => {
if (button.classList.contains('operator-buttons-clicked')) {
button.classList.toggle('operator-buttons-clicked');
};
});
}
function setOperator(button) {
if (button.textContent === '=') return; //prevents the operator from being set to '=', but removes 'operator-buttons-clicked' class when equal button is pressed
if (firstValueSet && operator === '' && displayValue !== '') { //enables a new calculation after the equal button was pressed
firstValueSet = false;
setValue(displayValue);
};
if (firstValueSet && operator !== '' && displayValue === '') { //makes it possible to change the operator
operator = lastOperator = button.textContent;
button.classList.toggle('operator-buttons-clicked');
return;
};
if (firstValueSet && operator !== '') { //handles the case if an operator key is pressed to concatenate calculations
setValue(displayValue);
firstValue = calculate(firstValue, operator, secondValue);
populate(firstValue);
displayValue = '';
} else {
setValue(displayValue);
};
operator = lastOperator = button.textContent;
button.classList.toggle('operator-buttons-clicked');
}
function operate() {
if (firstValue === undefined) return; //error handling if the equal key is pressed without values being set
if (operator === '') { //makes it possible to press the equal key several times to repeat the calculation
operator = lastOperator;
displayValue = '';
} else {
setValue(displayValue);
};
firstValue = calculate(firstValue, operator, secondValue);
if (isNaN(firstValue)) { //error handling if the equal key is pressed without values being set
populate('Error');
firstValueSet = false;
} else {
populate(firstValue);
};
displayValue = '';
}
//event listener
inputButtons.forEach(button => { //shows pressed numbers
button.addEventListener('click', () => populate(button.textContent) );
});
operatorButtons.forEach(button => {
button.addEventListener('click', () => {
toggleClickedClass();
setOperator(button);
})
});
equalButton.addEventListener('click', operate);
clearButton.addEventListener('click', clear);
backspaceButton.addEventListener('click', backspace);
negateButton.addEventListener('click', negateInputValue);
//KEYBOARD SUPPORT
let key = 0;
//event listener
window.addEventListener('keydown', useKeyboardInput);
//negateButton function call and 'active' class toggle
window.addEventListener('keydown', (e) => {
if (e.code === 'Slash' && e.altKey) {
negateInputValue();
negateButton.classList.add('active');
};
});
window.addEventListener('keyup', (e) => {
if (e.code === 'Slash' && e.altKey) {
negateButton.classList.remove('active');
};
});
//toggles 'active' class on pressed key
window.addEventListener('keydown', () => {
if (!key) return;
key.classList.add('active');
});
window.addEventListener('keyup', () => {
if (!key) return;
key.classList.remove('active');
});
window.addEventListener('keydown', useKeyboardInput);
function useKeyboardInput(e) {
key = document.querySelector(`button[data-key="${e.key}"]`)
switch (true) {
case (!key):
break;
case (key.classList.contains('input-buttons')):
populate(key.textContent);
break;
case (key.id === 'add'):
case (key.id === 'subtract'):
case (key.id === 'multiply'):
case (key.id === 'divide'):
toggleClickedClass();
setOperator(key);
break;
case (key.id === 'equal'):
toggleClickedClass();
operate();
break;
case (key.id === 'clear'):
clear();
break;
case (key.id === 'backspace'):
backspace();
break;
};
}