-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
227 lines (198 loc) · 6.04 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
function add(x, y) {
return Math.round(100 * (x + y)) / 100;
}
function subtract(x, y) {
return Math.round(100 * (x - y)) / 100;
}
function multiply(x, y) {
return Math.round(100 * x * y) / 100;
}
function divide(x, y) {
return y === 0 ? "Err!!" : Math.round(100 * x / y) / 100;
}
function modulus(x, y) {
return x % y;
}
let num1 = Number.MIN_SAFE_INTEGER;
let operator = '';
let num2 = Number.MIN_SAFE_INTEGER;
function operate(func, x, y) {
switch (func) {
case '+':
return add(x, y);
case '-':
return subtract(x, y);
case 'x':
return multiply(x, y);
case '/':
return divide(x, y);
case '%':
return modulus(x, y);
}
}
const numbers = document.querySelectorAll('.num');
const display = document.querySelector('.current');
const history = document.querySelector('.history');
const equals = document.querySelector('.equals');
const operators = document.querySelectorAll('.operator');
const fullClear = document.querySelector('.all-clear');
const singleClear = document.querySelector('.single-clear');
const btns = document.querySelectorAll('.btn');
const dotBtn = document.querySelector('.dot');
numbers.forEach(number => {
number.addEventListener('click', () => handleNumberInput(number.textContent));
});
operators.forEach(method => {
method.addEventListener('click', () => handleOperatorInput(method.textContent));
});
// Add hover over event on the buttons
btns.forEach(btn => {
btn.addEventListener('mouseover', () => {
if (btn.textContent === '.' && display.textContent.includes('.')) {
return;
}
btn.classList.add('btn-hover')
})
btn.addEventListener('mouseout', () => {
btn.classList.remove('btn-hover')
})
})
function handleNumberInput(input) {
// console.log(`num1: ${num1} op: ${operator} num2: ${num2}`);
// handling two dots
if (input === '.' && display.textContent.includes('.')) {
return;
}
// handle big numbers
if (display.textContent.length > 8 || history.textContent.length > 13) {
display.textContent = 'Too Large';
return;
}
// if input is .
if (input === '.') {
if (display.textContent.length === 0 || isNaN(parseFloat(display.textContent))) {
// if display is empty or display is non number
display.textContent = '0.';
populateNumber(0);
} else {
// display is not empty
display.textContent += input;
populateNumber(display.textContent.slice(0, -1));
}
} else {
// else input is a number
if (!isNaN(parseFloat(display.textContent)) && display.textContent !== '0') {
// if display already contain a number
display.textContent += input;
} else {
display.textContent = input;
}
populateNumber(parseFloat(display.textContent));
}
}
function handleOperatorInput(input) {
// console.log(`num1: ${num1} op: ${operator} num2: ${num2}`);
// input is an operator
if (input === '=') {
calculate(input);
} else {
display.textContent = input;
populateOperator(input);
}
}
function populateNumber(num) {
if (operator === '') {
num1 = num;
} else {
num2 = num;
}
}
function populateOperator(input) {
if (operator === '' || num2 === Number.MIN_SAFE_INTEGER) {
operator = input;
} else {
calculate(input);
}
}
function calculate(input) {
// if any number is empty for this call send error
if (num1 === Number.MIN_SAFE_INTEGER || num2 === Number.MIN_SAFE_INTEGER) {
// do nothing rather than throwing error
// display.textContent = 'Err!!';
// resetVariables();
} else {
let result = operate(operator, num1, num2);
history.textContent = "Ans: " + result;
if (input === '=') {
display.textContent = result;
operator = '';
} else {
operator = input;
}
num1 = result;
num2 = Number.MIN_SAFE_INTEGER;
}
}
// full clear
fullClear.addEventListener('click', () => deleteAll());
function deleteAll() {
display.textContent = '0';
history.textContent = 'Ans: ';
resetVariables();
}
function resetVariables() {
num1 = Number.MIN_SAFE_INTEGER;
operator = '';
num2 = Number.MIN_SAFE_INTEGER;
}
// single clear
singleClear.addEventListener('click', () => deleteLastChar());
function deleteLastChar() {
// if display === 0, do nothing
if (display.textContent === '0') {
return;
}
// if last char === .
if (display.textContent.slice(-1) === '.') {
display.textContent = display.textContent.slice(0, -1);
return;
}
// if display contains a number
// check if num2 is empty,
// if num2 is empty, then last digit from num1
// else remove last digit from num2
if (!isNaN(parseFloat(display.textContent))) {
if (num2 === Number.MIN_SAFE_INTEGER) {
num1 = num1 / 10;
} else {
num2 = num2 / 10;
}
if (display.textContent.length === 1) {
display.textContent = 0;
} else {
display.textContent = display.textContent.slice(0, -1);
}
} else {
// if display contains operator
operator = '';
display.textContent = display.textContent.slice(0, -1);
}
}
// keyboard support
document.addEventListener('keydown', (event) => {
let pressedKey = event.key;
pressedKey = pressedKey === 'Enter' ? '=' : pressedKey;
pressedKey = pressedKey === '*' ? 'x' : pressedKey;
let operatorKeys = 'x+-/=%';
if (!isNaN(parseInt(pressedKey)) || pressedKey === '.') {
// if input is number
handleNumberInput(pressedKey);
} else if (operatorKeys.includes(pressedKey)) {
// operator key
handleOperatorInput(pressedKey);
} else if (pressedKey === 'Backspace') {
deleteLastChar();
} else if (pressedKey === 'Delete') {
deleteAll();
}
});