-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
114 lines (109 loc) · 4.72 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
const mainDisplay = document.getElementById('main-display');
const secondaryDisplay = document.getElementById('secondary-display');
const buttons = document.querySelectorAll('.button');
let currentInput = '';
let secondaryInput = '';
let operator = '';
let resultCalculated = false;
let history = []; // Array to store history
function calculateExpression(expression) {
try {
// Replace operators for eval function
return eval(expression.replace('×', '*').replace('÷', '/').replace('^', '**'));
} catch {
return 'Error';
}
}
function updateHistory(expression, result) {
history.push(`${expression} = ${result}`);
// Optionally, you can limit history length
if (history.length > 10) history.shift(); // Keep last 10 entries
}
buttons.forEach(button => {
button.addEventListener('click', () => {
const value = button.textContent;
if (!isNaN(value) || value === '.') {
if (resultCalculated) {
currentInput = value;
resultCalculated = false;
} else {
currentInput += value;
}
mainDisplay.value = currentInput;
} else if (value === 'C') {
currentInput = '';
secondaryInput = '';
operator = '';
mainDisplay.value = '';
secondaryDisplay.value = '';
} else if (value === '±') {
currentInput = currentInput ? (-parseFloat(currentInput)).toString() : '';
mainDisplay.value = currentInput;
} else if (value === 'π') {
currentInput = Math.PI.toString();
mainDisplay.value = currentInput;
} else if (value === '√') { // Square root
if (currentInput) {
currentInput = Math.sqrt(parseFloat(currentInput)).toString();
mainDisplay.value = currentInput;
}
} else if (value === '=') {
if (currentInput && secondaryInput) {
// Concatenate current input to secondary input
secondaryInput += currentInput;
const result = calculateExpression(secondaryInput);
mainDisplay.value = result; // Display result in main display
updateHistory(secondaryInput, result); // Update history
secondaryDisplay.value = `${secondaryInput} = ${result}`; // Show last calculation
// Reset for next calculation
currentInput = ''; // Clear current input
secondaryInput = ''; // Clear secondary input for new expression
operator = ''; // Reset operator
resultCalculated = true; // Indicate that a result has been calculated
}
} else if (['+', '−', '×', '÷', '^'].includes(value)) {
if (currentInput) {
if (resultCalculated) {
secondaryInput = currentInput + value; // Start a new expression
resultCalculated = false;
} else {
secondaryInput += currentInput + value; // Append to existing expression
}
secondaryDisplay.value = secondaryInput.replace('×', '*').replace('÷', '/');
currentInput = ''; // Clear current input
mainDisplay.value = ''; // Clear main display
}
} else if (['sin', 'cos', 'tan', 'log', 'exp'].includes(value)) {
if (currentInput) {
let computedValue;
switch (value) {
case 'sin':
computedValue = Math.sin(parseFloat(currentInput) * Math.PI / 180);
break;
case 'cos':
computedValue = Math.cos(parseFloat(currentInput) * Math.PI / 180);
break;
case 'tan':
computedValue = Math.tan(parseFloat(currentInput) * Math.PI / 180);
break;
case 'log':
computedValue = Math.log10(parseFloat(currentInput));
break;
case 'exp': // Exponential
computedValue = Math.exp(parseFloat(currentInput));
break;
}
currentInput = computedValue.toString();
mainDisplay.value = currentInput;
}
}
});
});
// Keyboard support
document.addEventListener('keydown', (event) => {
const key = event.key;
const button = Array.from(buttons).find(btn => btn.textContent === key);
if ( button) {
button.click();
}
});