-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcal.js
30 lines (28 loc) · 790 Bytes
/
cal.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
let currentInput = "";
let expression = "";
const display = document.getElementById("display");
const buttons = document.querySelectorAll('button');
buttons.forEach((button) => {
button.addEventListener('click', (e) => {
const buttonText = e.target.textContent;
if (buttonText === '=') {
try {
expression += currentInput;
currentInput = eval(expression).toString();
display.value = currentInput;
expression = "";
} catch (error) {
display.value = "Error";
expression = "";
currentInput = "";
}
} else if (buttonText === 'C') {
currentInput = "";
expression = "";
display.value = "";
} else {
currentInput += buttonText;
display.value = currentInput;
}
});
});