-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
51 lines (48 loc) · 1.32 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
let displayNumber = document.querySelector(".display_number");
let displayOp = document.querySelector(".display_op");
let click = document.querySelectorAll(".row div");
let clear = document.querySelector("#f1");
let delet = document.querySelector("#f2");
let ar = ["÷", "x", "-", "+", "."];
let val = [];
let value = 0;
let op = "";
click.forEach((item) => {
item.addEventListener("click", () => {
if (ar.indexOf(item.innerText) > -1) {
op = item.innerText;
displayOp.innerText = op;
console.log(op);
val.push(value);
value = 0;
} else {
if (item.innerText == "=") {
val.push(value);
console.log(val);
value = 0;
displayOp.innerText = "";
if (op == "÷") {
value = val[0] / val[1];
} else if (op == "x") {
value = val[0] * val[1];
} else if (op == "-") {
value = val[0] - val[1];
} else if (op == "+") {
value = val[0] + val[1];
}
displayNumber.innerText = value;
val = [value];
} else {
value = value * 10 + parseInt(item.innerText);
displayNumber.innerText = value;
console.log(value);
}
}
});
});
clear.addEventListener("click", () => {
displayNumber.innerText = 0;
displayOp.innerText = "";
value = 0;
val = [];
});