-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
326 lines (290 loc) · 9.58 KB
/
Copy pathscript.js
File metadata and controls
326 lines (290 loc) · 9.58 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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
class Calculator {
constructor() {
this.previousOperandElement = document.getElementById('previous-operand');
this.currentOperandElement = document.getElementById('current-operand');
this.isScientific = false;
this.isRadians = false;
this.clear();
}
clear() {
this.currentOperand = '0';
this.previousOperand = '';
this.operation = undefined;
this.updateDisplay();
}
delete() {
if (this.currentOperand === '0') return;
if (this.currentOperand.length === 1) {
this.currentOperand = '0';
} else {
this.currentOperand = this.currentOperand.toString().slice(0, -1);
}
this.updateDisplay();
}
appendNumber(number) {
if (number === '.' && this.currentOperand.includes('.')) return;
if (this.currentOperand === '0' && number !== '.') {
this.currentOperand = number.toString();
} else {
this.currentOperand = this.currentOperand.toString() + number.toString();
}
this.updateDisplay();
}
chooseOperation(operation) {
if (this.currentOperand === '0' && this.previousOperand === '') return;
if (this.previousOperand !== '') {
this.compute();
}
this.operation = operation;
this.previousOperand = this.currentOperand;
this.currentOperand = '0';
this.updateDisplay();
}
compute() {
let computation;
const prev = parseFloat(this.previousOperand);
const current = parseFloat(this.currentOperand);
if (isNaN(prev) || isNaN(current)) return;
switch (this.operation) {
case '+':
computation = prev + current;
break;
case '-':
computation = prev - current;
break;
case '×':
computation = prev * current;
break;
case '÷':
if (current === 0) {
alert("Cannot divide by zero!");
return;
}
computation = prev / current;
break;
default:
return;
}
this.currentOperand = this.formatResult(computation);
this.operation = undefined;
this.previousOperand = '';
this.updateDisplay();
}
sin() {
const value = this.isRadians ? parseFloat(this.currentOperand) : (parseFloat(this.currentOperand) * Math.PI / 180);
this.currentOperand = this.formatResult(Math.sin(value));
this.updateDisplay();
}
cos() {
const value = this.isRadians ? parseFloat(this.currentOperand) : (parseFloat(this.currentOperand) * Math.PI / 180);
this.currentOperand = this.formatResult(Math.cos(value));
this.updateDisplay();
}
tan() {
const value = this.isRadians ? parseFloat(this.currentOperand) : (parseFloat(this.currentOperand) * Math.PI / 180);
this.currentOperand = this.formatResult(Math.tan(value));
this.updateDisplay();
}
log() {
const value = parseFloat(this.currentOperand);
if (value <= 0) {
alert("Cannot calculate logarithm of zero or negative numbers!");
return;
}
this.currentOperand = this.formatResult(Math.log10(value));
this.updateDisplay();
}
ln() {
const value = parseFloat(this.currentOperand);
if (value <= 0) {
alert("Cannot calculate natural logarithm of zero or negative numbers!");
return;
}
this.currentOperand = this.formatResult(Math.log(value));
this.updateDisplay();
}
sqrt() {
const value = parseFloat(this.currentOperand);
if (value < 0) {
alert("Cannot calculate square root of negative numbers!");
return;
}
this.currentOperand = this.formatResult(Math.sqrt(value));
this.updateDisplay();
}
pow() {
const value = parseFloat(this.currentOperand);
this.currentOperand = this.formatResult(Math.pow(value, 2));
this.updateDisplay();
}
pi() {
this.currentOperand = Math.PI.toString();
this.updateDisplay();
}
e() {
this.currentOperand = Math.E.toString();
this.updateDisplay();
}
fact() {
const num = parseInt(this.currentOperand);
if (num < 0) {
alert("Cannot calculate factorial of negative numbers!");
return;
}
if (num > 170) {
alert("Number too large for factorial calculation!");
return;
}
let result = 1;
for (let i = 2; i <= num; i++) result *= i;
this.currentOperand = this.formatResult(result);
this.updateDisplay();
}
exp() {
this.currentOperand += 'e+';
this.updateDisplay();
}
toggleRad() {
this.isRadians = !this.isRadians;
const radButton = document.querySelector('[data-scientific="rad"]');
radButton.textContent = this.isRadians ? 'DEG' : 'RAD';
}
formatResult(number) {
if (number > 1e16 || number < -1e16) {
return number.toExponential(10);
}
return Math.round(number * 1e10) / 1e10;
}
updateDisplay() {
this.currentOperandElement.textContent = this.getDisplayNumber(this.currentOperand);
if (this.operation != null) {
this.previousOperandElement.textContent =
`${this.getDisplayNumber(this.previousOperand)} ${this.operation}`;
} else {
this.previousOperandElement.textContent = '';
}
}
getDisplayNumber(number) {
const stringNumber = number.toString();
const integerDigits = parseFloat(stringNumber.split('.')[0]);
const decimalDigits = stringNumber.split('.')[1];
let integerDisplay;
if (isNaN(integerDigits)) {
integerDisplay = '0';
} else {
integerDisplay = integerDigits.toLocaleString('en');
}
if (decimalDigits != null) {
return `${integerDisplay}.${decimalDigits}`;
} else {
return integerDisplay;
}
}
}
// Initialize calculator
const calculator = new Calculator();
// Add event listeners for basic operations
document.querySelectorAll('[data-number]').forEach(button => {
button.addEventListener('click', () => {
calculator.appendNumber(button.textContent);
});
});
document.querySelectorAll('[data-operator]').forEach(button => {
button.addEventListener('click', () => {
calculator.chooseOperation(button.textContent);
});
});
document.querySelector('[data-action="calculate"]').addEventListener('click', () => {
calculator.compute();
});
document.querySelector('[data-action="clear"]').addEventListener('click', () => {
calculator.clear();
});
document.querySelector('[data-action="delete"]').addEventListener('click', () => {
calculator.delete();
});
// Add keyboard support
document.addEventListener('keydown', (e) => {
if (e.key >= '0' && e.key <= '9' || e.key === '.') {
calculator.appendNumber(e.key);
}
if (e.key === '+' || e.key === '-') {
calculator.chooseOperation(e.key);
}
if (e.key === '*') {
calculator.chooseOperation('×');
}
if (e.key === '/') {
calculator.chooseOperation('÷');
}
if (e.key === 'Enter' || e.key === '=') {
e.preventDefault();
calculator.compute();
}
if (e.key === 'Backspace') {
calculator.delete();
}
if (e.key === 'Escape') {
calculator.clear();
}
});
// Add scientific mode toggle
const modeToggle = document.querySelector('.mode-toggle');
const calculatorElement = document.querySelector('.calculator');
modeToggle.addEventListener('click', () => {
calculator.isScientific = !calculator.isScientific;
calculatorElement.classList.toggle('scientific');
modeToggle.textContent = calculator.isScientific ? 'Basic' : 'Scientific';
});
// Add theme toggle
const themeToggle = document.querySelector('.theme-toggle');
themeToggle.addEventListener('click', () => {
const root = document.documentElement;
const isDark = root.getAttribute('data-theme') === 'dark';
root.setAttribute('data-theme', isDark ? '' : 'dark');
themeToggle.textContent = isDark ? 'Dark Mode' : 'Light Mode';
});
// Add scientific button listeners
document.querySelectorAll('[data-scientific]').forEach(button => {
button.addEventListener('click', () => {
const operation = button.getAttribute('data-scientific');
switch (operation) {
case 'sin':
calculator.sin();
break;
case 'cos':
calculator.cos();
break;
case 'tan':
calculator.tan();
break;
case 'log':
calculator.log();
break;
case 'ln':
calculator.ln();
break;
case 'sqrt':
calculator.sqrt();
break;
case 'pow':
calculator.pow();
break;
case 'pi':
calculator.pi();
break;
case 'e':
calculator.e();
break;
case 'fact':
calculator.fact();
break;
case 'exp':
calculator.exp();
break;
case 'rad':
calculator.toggleRad();
break;
}
});
});