-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
92 lines (61 loc) · 1.99 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
let display = document.getElementById("display");
let buttons = Array.from(document.getElementsByClassName("button"));
buttons.map((button) => {
button.addEventListener("click", (e) => {
let clickedBtn = e.target.innerText;
// console.log("Content :- ", clickedBtn);
switch (clickedBtn) {
case "C":
display.innerText = "";
break;
case "=":
// try {
// display.innerText = eval(display.innerText);
// } catch {
// display.innerText = "Error";
// }
break;
default:
display.innerText += clickedBtn;
}
});
});
buttons.map((button) => {
button.addEventListener("click", (e) => {
let clickedBtn = e.target.innerText;
// console.log("Content :- ", clickedBtn);
var myVal = document.getElementById("display").innerText;
console.log(myVal);
let answer = document.getElementById("input_answer");
if (myVal != "") {
answer.innerText = "Ans: " + eval(myVal);
}
if (myVal == "") {
answer.innerText = "";
}
switch (clickedBtn) {
case "C":
display.innerText = "";
break;
case "=":
try {
if(display.innerText != ""){
answer.innerText = "Ans: "+eval(display.innerText);
}
} catch {
answer.innerText = "Error";
}
break;
default:
//.innerText += clickedBtn;
}
});
});
// display.addEventListener('change', calculate);
// function calculate() {
// try {
// display.innerText = eval(display.innerText);
// } catch {
// display.innerText = "Error";
// }
// }