-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
32 lines (27 loc) · 879 Bytes
/
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
const display = document.querySelector('.display');
const buttons = document.querySelectorAll('button');
let output = "";
const specialChars = ['/', '+', '-', '*'];
const calculate = (btnValue) => {
if (btnValue === '=' && output !== '') {
output = eval(output);
} else if (btnValue === "DEL") {
output = output.toString().slice(0, -1);
} else if (output === "0" && !specialChars.includes(btnValue)) {
output = btnValue;
} else {
if (output === "" && specialChars.includes(btnValue)) return;
output += btnValue;
}
display.value = output;
}
buttons.forEach((button) => {
button.addEventListener('click', (event) => {
calculate(event.target.value);
});
});
const resetButton = document.querySelector('.reset');
resetButton.addEventListener('click', () => {
output = "0";
display.value = output;
});