-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
201 lines (165 loc) · 6.22 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
const numberBtns = document.querySelectorAll('.number');
const operatorBtns = document.querySelectorAll('.operator');
const equalBtn = document.querySelector('.equal');
const clearBtn = document.querySelector('.clear');
const deleteBtn = document.querySelector('.delete');
const decimalPointBtn = document.querySelector('.decimal-point');
const plusMinusBtn = document.querySelector('.plus-minus');
const screenPrevText = document.querySelector('.screen-previous');
const screenCurrText = document.querySelector('.screen-current');
// Initialize starting state
let operand1 = "";
let operand2 = "";
let operator = null;
let haveResult = false;
numberBtns.forEach(button => {
button.addEventListener('click', () => clickNumber(button.textContent));
})
function clickNumber(numButtonText) {
// If a result has previously been computed
if (haveResult) {
// Reinitialize variables for a new calculation
// Give current screen value of newly pressed number button and reset previous screen display
screenCurrText.textContent = numButtonText;
screenPrevText.textContent= '';
clearVars();
return;
}
// If number button clicked is 0, retain screen display as '0'
if (screenCurrText.textContent === '0') screenCurrText.textContent = numButtonText;
// Otherwise, append number button value to current screen display
else screenCurrText.textContent += numButtonText;
}
operatorBtns.forEach(button => {
button.addEventListener('click', () => clickOperator(button.textContent));
})
function clickOperator(operatorBtnText) {
if (haveResult) {
// Function checks if an error exists
// Terminates upper nested function if there is an error
isError();
let result = screenCurrText.textContent;
operand1 = result;
operand2 = "";
operator = operatorBtnText;
screenPrevText.textContent= `${result} ${operator}`;
screenCurrText.textContent = '0';
haveResult = false;
return;
}
// If an operator button is clicked for the first time
if (!operand1) {
operand1 = screenCurrText.textContent;
operator = operatorBtnText;
screenPrevText.textContent = `${operand1} ${operatorBtnText}`;
screenCurrText.textContent = '0';
}
// If an operator button is clicked again successively
else {
// Replace value of old operator with the new one
operator = operatorBtnText;
screenPrevText.textContent = `${operand1} ${operatorBtnText}`;
}
}
equalBtn.addEventListener('click', () => clickEqual());
function clickEqual() {
if (haveResult) {
isError();
// If result has been calculated, repeatedly clicking on equal button will operate on result ...
operand1 = screenCurrText.textContent;
let result = operate(operator, operand1, operand2);
screenCurrText.textContent = result;
screenPrevText.textContent = `${operand1} ${operator} ${operand2} = ${result}`;
return;
}
operand2 = screenCurrText.textContent;
// Only calculate if operand 1, operand 2 and operator have values
if (operand1 && operand2 && operator) {
let result = operate(operator, operand1, operand2);
screenCurrText.textContent = result;
screenPrevText.textContent = `${operand1} ${operator} ${operand2} = ${result}`;
haveResult = true;
}
}
clearBtn.addEventListener('click', () => {
fullClear();
});
function clearVars() {
operand1 = "";
operand2 = "";
operator = null;
haveResult = false;
}
function fullClear() {
screenCurrText.textContent = '0';
screenPrevText.textContent= '';
clearVars();
}
// Function handles divide by 0 scenario when result becomes 'Error'
function isError() {
if (screenCurrText.textContent === 'Error') {
fullClear();
throw new Error("Error value found in lower screen display");
}
}
deleteBtn.addEventListener('click', clickDeleteBtn);
function clickDeleteBtn() {
isError();
if (haveResult) return;
if (screenCurrText.textContent.length === 1) screenCurrText.textContent = '0';
else screenCurrText.textContent = screenCurrText.textContent.slice(0, -1);
if (screenCurrText.textContent === '-') screenCurrText.textContent = '0';
}
plusMinusBtn.addEventListener('click', clickPlusMinus);
function clickPlusMinus() {
isError();
if (haveResult) return;
screenCurrText.textContent = `${Number(screenCurrText.textContent) * -1}`;
}
decimalPointBtn.addEventListener('click', clickDecimalPoint);
function clickDecimalPoint() {
isError();
if (haveResult) return;
if (!screenCurrText.textContent.includes('.')) screenCurrText.textContent += '.';
}
// Add keyboard support
document.addEventListener('keydown', typeKey);
function typeKey(e) {
let operators = ['+', '-', '/', '*', '%']
if (e.key >= 0 && e.key <= 9) clickNumber(e.key);
if (operators.includes(e.key)) clickOperator(convertOperator(e.key));
if (e.key === '=' || e.key === 'Enter') clickEqual();
if (e.key === 'Escape') fullClear();
if (e.key === 'Backspace') clickDeleteBtn();
if (e.key.toLowerCase() === 'p' || e.key.toLowerCase() === 'm') clickPlusMinus();
if (e.key === '.') clickDecimalPoint();
}
function convertOperator(keyOperator) {
if (keyOperator === '+' || keyOperator === '-' || keyOperator === '%') return keyOperator;
else if (keyOperator === '*') return '×';
else if (keyOperator === '/') return '÷';
}
// Add function for commas ???
// Add function for screen overflow
// Add function for rounding
// Fix floating point division error 0.2 x 3 = 0.6 not 0.600001
// IF result found, disable all other buttons, e.g. delete, plus-minus
// Add textbox to introduce keyboard support
function operate(operator, operand1, operand2) {
let num1 = Number(operand1);
let num2 = Number(operand2);
switch (operator) {
case "+":
return num1 + num2;
case "-":
return num1 - num2;
case "×":
return num1 * num2;
case "÷":
if (num2 === 0) return 'Error';
else return num1 / num2;
case "%":
if (num2 === 0) return 'Error';
else return num1 % num2;
}
}