-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript. js
More file actions
95 lines (83 loc) · 2.56 KB
/
Copy pathscript. js
File metadata and controls
95 lines (83 loc) · 2.56 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
'&// Get the display element
const display = document.getElementById('display');
// Append number to display
function appendNumber(num) {
if (display.value.length < 50) {
display.value += num;
}
}
// Append operator to display
function appendOperator(op) {
if (display.value && !display.value.endsWith(op)) {
display.value += op;
}
}
// Append function to display
function appendFunction(func) {
if (display.value.length < 50) {
display.value += func;
}
}
// Append pi constant
function appendPi() {
if (display.value.length < 50) {
display.value += Math.PI.toString();
}
}
// Clear display
function clearDisplay() {
display.value = '';
}
// Delete last character
function deleteLast() {
display.value = display.value.slice(0, -1);
}
// Calculate result
function calculate() {
try {
let expression = display.value;
// Replace mathematical functions with JavaScript equivalents
expression = expression.replace(/sin\(/g, 'Math.sin(');
expression = expression.replace(/cos\(/g, 'Math.cos(');
expression = expression.replace(/tan\(/g, 'Math.tan(');
expression = expression.replace(/sqrt\(/g, 'Math.sqrt(');
expression = expression.replace(/log\(/g, 'Math.log10(');
expression = expression.replace(/ln\(/g, 'Math.log(');
expression = expression.replace(/pow\(/g, 'Math.pow(');
// Evaluate the expression
const result = eval(expression);
// Display result with limited decimal places
if (typeof result === 'number') {
display.value = parseFloat(result.toFixed(10));
} else {
display.value = result;
}
} catch (error) {
display.value = 'Error';
console.error('Calculation error:', error);
}
}
// Allow keyboard input
document.addEventListener('keydown', function(event) {
const key = event.key;
if (key >= '0' && key <= '9') {
appendNumber(key);
} else if (key === '+' || key === '-' || key === '*' || key === '/') {
appendOperator(key);
} else if (key === '.' || key === ',') {
appendNumber('.');
} else if (key === 'Enter' || key === '=') {
event.preventDefault();
calculate();
} else if (key === 'Backspace') {
event.preventDefault();
deleteLast();
} else if (key === 'Escape') {
event.preventDefault();
clearDisplay();
} else if (key === '(') {
appendNumber('(');
} else if (key === ')') {
appendNumber(')');
}
});