-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
66 lines (57 loc) · 1.43 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
let digits = document.querySelector("#digits");
let operand1 = 0;
let operator = "";
let isNew = false;
let elements = document.querySelectorAll(".btns");
elements.forEach(function (element) {
element.addEventListener("click", clickHandler);
});
let operate = document.querySelectorAll(".operate");
operate.forEach(function (element) {
element.addEventListener("click", operation);
});
function operation(e) {
let target = e.target;
if (target.innerText == "=") {
let operand2 = parseInt(digits.innerText);
let resultData;
switch (operator) {
case "+":
resultData = operand1 + operand2;
break;
case "-":
resultData = operand1 - operand2;
break;
case "*":
resultData = operand1 * operand2;
break;
case "/":
resultData = operand1 / operand2;
break;
}
digits.innerText = resultData;
operand1 = 0;
isNew = true;
operator = "";
} else {
operand1 = parseInt(digits.innerText);
operator = target.innerText;
isNew = true;
}
}
function clickHandler(e) {
if (digits.innerText == "0" || isNew) {
digits.innerText = "";
isNew = false;
}
let target = e.target;
digits.innerText += target.innerText;
}
let ac = document.getElementById("ac");
ac.addEventListener("click", function () {
let target = document.getElementById("digits");
target.innerText = 0;
operand1 = 0;
operator = "";
isNew = false;
});