-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain.js
More file actions
35 lines (30 loc) · 866 Bytes
/
main.js
File metadata and controls
35 lines (30 loc) · 866 Bytes
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
const form = document.getElementById('myform');
// Function to append value to the display
function appendToDisplay(value) {
form.display.value += value;
}
// Function to clear the display
function clearDisplay() {
form.display.value = '';
}
// Function to evaluate the expression in the display
function calculate() {
try {
form.display.value = eval(form.display.value.replace(/x/g, '*'));
} catch (error) {
form.display.value = 'Error';
}
}
// Assign event listeners to buttons
document.querySelectorAll('input[type=button]').forEach(button => {
button.addEventListener('click', function() {
const value = this.value;
if (value === 'C') {
clearDisplay();
} else if (value === '=') {
calculate();
} else {
appendToDisplay(value);
}
});
});