-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
55 lines (47 loc) · 1.3 KB
/
script.js
File metadata and controls
55 lines (47 loc) · 1.3 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
let display = document.getElementById("display")
let buttons = document.querySelectorAll("button")
let operators = ["+", "-", "*", "/", "%"];
buttons.forEach(button => {
button.addEventListener("click", (e) => {
value = e.target.innerText
let last = display.value.slice(-1)
if (display.value === "Invalid Input") {
display.value = "0"
}
else if (value === "AC") {
display.value = "0"
}
else if (value === "=") {
calculate()
}
else if (value === "DEL") {
display.value = display.value.slice(0, -1)
if (display.value === "") {
display.value = "0"
}
}
else if (operators.includes(value)) {
if (operators.includes(last)) {
display.value = display.value.slice(0, -1) + value
return
}
else {
display.value += value
return
}
}
else if (display.value === "0") {
display.value = value
}
else {
display.value += value
}
})
});
function calculate() {
try {
display.value = eval(display.value)
} catch (err) {
display.value = "Invalid Input"
}
}