-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
229 lines (174 loc) · 5.32 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
let screen = document.querySelector('.display');
let display = document.getElementById('typed');
let answer = document.getElementById('answer');
let number = document.querySelectorAll('.number');
let operator = document.querySelectorAll('.operator');
let clear = document.getElementById('clear');
let backspace = document.getElementById('backspace');
let decimal = document.getElementById('.');
let evaluate = document.getElementById('=');
let currentOperator = '';
let a = null;
let b = null;
let displayed = '';
const add = (a, b) => a + b;
const subtract = (a, b) => a - b;
const multiply = (a, b) => a * b;
const divide = (a, b) => a / b;
const operate = (operator, a, b) => {
return (operator == '+') ? add(a,b)
: (operator == '-') ? subtract(a,b)
: (operator == '*') ? multiply(a,b)
: (operator == '/') ? divide(a,b)
: "ERROR"
};
const addDecimal = (e) => {
displayed += e.target.id;
display.textContent = displayed;
};
const testResult = (result) =>{
//format result if it's a decimal
let stringResult = String(result);
console.log(a, currentOperator, b, stringResult);
let formattedNum = '';
if (stringResult.includes('.')) {
formattedNum = result.toFixed(3);
result = formattedNum;
}
if (currentOperator == '/' && b == 0) {
answer.textContent = `undefined`;
return false;
}
if (isNaN(result)) {
answer.textContent = 'Math error';
return false;
} else if (result === 'ERROR') {
answer.textContent = 'syntax error';
return false;
} else {
answer.textContent = result;
return result;
}
};
const displayOperator = (e) => {
if (!currentOperator) {
a = Number(displayed);
currentOperator = e.target.id;
displayed += currentOperator;
answer.textContent = '';
} else if (currentOperator && !a) {
a = Number(answer.textContent);
currentOperator = e.target.id;
displayed = a + currentOperator;
} else {
let answer = getAnswer();
let result = testResult(answer);
if (result) {
displayResult(result);
a = Number(result);
currentOperator = e.target.id;
displayed = a + currentOperator;
answer.textContent = '';
} else {
a = null;
b = null;
displayed = '';
currentOperator = '';
}
}
display.textContent = displayed;
};
const getResult = () => {
let answer = getAnswer();
let result = testResult(answer);
if (result) {
displayResult(result);
} else {
a = null;
b = null;
displayed = '';
currentOperator = '';
}
display.textContent = displayed;
}
const displayResult = (result) => {
answer.textContent = result;
a = null;
b = null;
}
const displayNumber = (e) => {
if (answer.textContent && !a) {
displayed = '';
currentOperator = '';
a = Number(e.target.id);
}
displayed += e.target.id;
display.textContent = displayed;
};
const clearScreen = () => {
answer.textContent = '';
a = null;
b = null;
displayed = '';
display.textContent = '';
currentOperator = '';
};
const getAnswer = () => {
let excludeNegatives = displayed.slice(1);
let index = excludeNegatives.indexOf(currentOperator);
b = Number(excludeNegatives.slice(index+1));
if (a && currentOperator && b) {
let answer = operate(currentOperator, a, b);
return Number(answer);
}
};
const undoLast = () => {
let newString = displayed.slice(0, -1);
displayed = newString;
display.textContent = newString;
}
number.forEach(button => button.addEventListener('click', displayNumber));
operator.forEach(button => button.addEventListener('click', displayOperator));
clear.addEventListener('click', clearScreen);
backspace.addEventListener('click', undoLast);
decimal.addEventListener('click', addDecimal);
evaluate.addEventListener('click', getResult);
/*
currentButton = e.target.textContent;
let numbers = ['1','2','3','4','5','6','7','8','9','0']
let operators = ['+', '-','*', '/'];
if (currentButton === '<--') backSpace();
if (currentButton === '.') addDecimal();
if (operators.includes(currentButton)) {
if (currentOperator) {
let array = displayed.split(' ');
console.log(array);
let a = Number(array[0]);
let b = Number(array[2]);
answer.textContent = operate(currentOperator, a, b);
currentOperator = '';
displayed = answer.textContent;
}
if (answer.textContent) {
displayed = answer.textContent;
}
currentOperator = currentButton;
displayed += ' ' + currentOperator + ' ';
}
if (numbers.includes(currentButton)) {
displayed += currentButton;
}
if (currentButton === '=') {
let array = displayed.split(' ');
console.log(array);
let a = Number(array[0]);
let b = Number(array[2]);
answer.textContent = operate(currentOperator, a, b);
currentOperator = '';
}
if (currentButton === 'C') {
displayed = '';
answer.textContent = '';
}
updatedDisplay.textContent = displayed;
*/