-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
155 lines (139 loc) · 3.74 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
let container = document.querySelector(".container");
let inpValue = document.querySelector(".inpValue");
let menuContainer = document.querySelector(".menu_container");
let button_operators = document.querySelectorAll(".button_operator");
let button_style = document.querySelectorAll(".button_style");
let firstValue = "";
let secondValue = "";
let firstState = false;
let secondState = false;
let sign = "";
let result = 0;
for (let i = 0; i < button_style.length; i++) {
button_style[i].addEventListener("click", (event) => {
if (
firstState === false &&
event.target.innerText >= "0" &&
event.target.innerText <= "9" &&
firstValue.length <= 9
) {
firstValue += event.target.innerText;
inpValue.value = firstValue;
}
/////////////////////////////////////////////
if (
secondState === true &&
event.target.innerText >= "0" &&
event.target.innerText <= "9" &&
secondValue.length <= 9
) {
secondValue += event.target.innerText;
inpValue.value = secondValue;
}
/////////////////////////////////////////////
if (
event.target.innerText === "/" ||
event.target.innerText === "X" ||
event.target.innerText === "-" ||
event.target.innerText === "+" ||
event.target.innerText === "="
) {
clearButton();
firstState = true;
secondState = true;
if (secondValue !== "") {
calculator();
}
sign = event.target.innerText;
}
/////////////////////////////////////////////
if (event.target.innerText === "AC") {
clearInput();
}
/////////////////////////////////////////////
if (event.target.innerText === "%" || event.target.innerText === "+/-") {
sign = event.target.innerText;
clearButton();
calculator();
}
/////////////////////////////////////////////
if (inpValue.value.length == "7") {
inpValue.style.fontSize = "60px";
} else if (inpValue.value.length >= "8") {
inpValue.style.fontSize = "50px";
} else {
inpValue.style.fontSize = "80px";
}
});
function calculator() {
let num1 = +firstValue;
let num2 = +secondValue;
if (sign === "+" && num2 !== "") {
result = num1 + num2;
firstValue = result;
}
if (sign === "-" && num2 !== "") {
result = num1 - num2;
firstValue = result;
}
if (sign === "/" && num2 !== "") {
result = num1 / num2;
firstValue = result;
}
if (sign === "X" && num2 !== "") {
result = num1 * num2;
firstValue = result;
}
if (sign === "+/-") {
if ((firstState = true)) {
firstValue = -num1;
result = firstValue;
} else {
firstValue = -num2;
result = firstValue;
}
}
if (sign === "=") {
inpValue.value = result;
}
if (sign === "%") {
if ((firstState = true)) {
firstValue = num1 / 100;
result = firstValue;
} else {
firstValue = num2 / 100;
result = firstValue;
}
}
secondValue = "";
inpValue.value = result;
}
}
currenyStyle(button_operators);
function clearInput() {
clearButton();
inpValue.value = "";
firstValue = "";
secondValue = "";
result = "";
firstState = false;
secondState = false;
}
function clearButton() {
button_operators.forEach((item) => {
item.style.backgroundColor = "#f69a01";
item.style.color = "white";
});
}
function currenyStyle(targetObject) {
targetObject.forEach((item) => {
item.addEventListener("click", () => {
targetObject.forEach((item) => {
item.style.backgroundColor = "#f69a01";
item.style.color = "white";
});
item.style.backgroundColor = "white";
item.style.color = "#f69a01";
});
});
}