-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
171 lines (155 loc) · 3.88 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const p = document.querySelector("p");
const numbers = document.querySelectorAll(".numbers");
const point = document.querySelector(".point");
const operators = document.querySelectorAll(".operators");
const equals = document.querySelector(".equal");
const ac = document.querySelector(".clear");
const percent = document.querySelector(".percent");
const sign = document.querySelector(".sign");
let holder1 = 0;
let holder2 = 0;
let result = 0;
let chosenOperator = "";
function add(x, y) {
return Math.round((x + y) * 100) / 100;
}
function subtract(x, y) {
return Math.round((x - y) * 100) / 100;
}
function multiply(x, y) {
return Math.round(x * y * 100) / 100;
}
function divide(x, y) {
return Math.round((x / y) * 100) / 100;
}
function operate(operator, int1, int2) {
if (operator === "+") {
return add(int1, int2);
} else if (operator === "-") {
return subtract(int1, int2);
} else if (operator === "×") {
return multiply(int1, int2);
} else if (operator === "÷") {
return divide(int1, int2);
}
}
function clear() {
holder1 = 0;
holder2 = 0;
chosenOperator = "";
}
function acClear() {
ac.addEventListener("click", () => {
clear();
p.textContent = "0";
result = 0;
});
}
function percentage() {
percent.addEventListener("click", () => {
if (Number(p.textContent)) {
result = divide(Number(p.textContent), 100);
p.textContent = limitLength(result);
}
});
}
function convertSign() {
sign.addEventListener("click", () => {
if (Number(p.textContent)) {
result = multiply(Number(p.textContent), -1);
p.textContent = limitLength(result);
}
});
}
function numberInput() {
numbers.forEach((number) => {
number.addEventListener("click", () => {
if (
result !== 0 ||
p.textContent === "0" ||
p.textContent === "+" ||
p.textContent === "-" ||
p.textContent === "×" ||
p.textContent === "÷"
) {
p.textContent = number.value;
result = 0;
} else if (p.textContent.length < 9) {
p.textContent += number.value;
}
});
});
}
function pointInput() {
point.addEventListener("click", () => {
if (
p.textContent.indexOf(".") === -1 &&
p.textContent.length < 9 &&
(Number(p.textContent) || p.textContent === "0")
) {
p.textContent += ".";
}
});
}
function operatorInput() {
operators.forEach((operator) => {
operator.addEventListener("click", () => {
if (
!holder1 &&
p.textContent !== "+" &&
p.textContent !== "-" &&
p.textContent !== "×" &&
p.textContent !== "÷"
) {
holder1 = Number(p.textContent);
p.textContent = operator.value;
chosenOperator = p.textContent;
} else if (
holder1 &&
p.textContent !== "+" &&
p.textContent !== "-" &&
p.textContent !== "×" &&
p.textContent !== "÷"
) {
holder2 = Number(p.textContent);
result = operate(chosenOperator, holder1, holder2);
holder1 = result;
p.textContent = operator.value;
chosenOperator = p.textContent;
}
});
});
}
function limitLength(int) {
if (int.toString().length > 9) {
return int.toExponential(2);
} else {
return int;
}
}
function equal() {
equals.addEventListener("click", () => {
if (
p.textContent === "+" ||
p.textContent === "-" ||
p.textContent === "×" ||
p.textContent === "÷"
) {
p.textContent = holder1;
} else if (chosenOperator && (!result || result === holder1)) {
holder2 = Number(p.textContent);
result = operate(chosenOperator, holder1, holder2);
p.textContent = limitLength(result);
} else if (result) {
p.textContent = limitLength(result);
}
clear();
});
}
percentage();
convertSign();
numberInput();
pointInput();
operatorInput();
acClear();
equal();