-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
194 lines (155 loc) · 4.39 KB
/
app.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Variables
const previousElement = document.querySelector(".previous-display");
const currentElement = document.querySelector(".current-display");
const acButton = document.querySelector(".ac");
const pmButton = document.querySelector(".pm");
const percentButton = document.querySelector(".percent");
const additionButton = document.querySelector(".addition");
const subtractionButton = document.querySelector(".subtraction");
const multiplicationButton = document.querySelector(".multiplication");
const divisionButton = document.querySelector(".division");
const equalsButton = document.querySelector(".equals");
const decimalButton = document.querySelector(".decimal");
const number0 = document.querySelector(".number-0");
const number1 = document.querySelector(".number-1");
const number2 = document.querySelector(".number-2");
const number3 = document.querySelector(".number-3");
const number4 = document.querySelector(".number-4");
const number5 = document.querySelector(".number-5");
const number6 = document.querySelector(".number-6");
const number7 = document.querySelector(".number-7");
const number8 = document.querySelector(".number-8");
const number9 = document.querySelector(".number-9");
const numbersArray = [
number0,
number1,
number2,
number3,
number4,
number5,
number6,
number7,
number8,
number9,
];
let previousOperand = "";
let currentOperand = "";
let operation = undefined;
let temporaryOperand = "";
// Functions
function DisplayNumbers() {
if (operation) {
previousElement.innerHTML = `${previousOperand} ${operation}`;
} else {
previousElement.innerHTML = previousOperand;
}
currentElement.innerHTML = currentOperand;
}
function AppendNumber(number) {
if (number === "." && currentOperand.includes(".")) return;
if (number === 0 && currentOperand === "0") return;
if (currentOperand.length > 7) return;
currentOperand = currentOperand.toString() + number.toString();
DisplayNumbers();
}
function ChooseOperation(selectedOperation) {
if (temporaryOperand) {
previousOperand = temporaryOperand.toString();
currentOperand = "";
temporaryOperand = "";
operation = selectedOperation;
DisplayNumbers();
return;
}
operation = selectedOperation;
previousOperand = currentOperand;
acButton.innerHTML = "AC";
currentOperand = "";
DisplayNumbers();
}
function Compute() {
let computation;
const previous = parseFloat(previousOperand);
const current = parseFloat(currentOperand);
if (!operation) return;
if (isNaN(previous) || isNaN(current)) return;
switch (operation) {
case "+":
computation = previous + current;
break;
case "-":
computation = previous - current;
break;
case "÷":
computation = previous / current;
break;
case "*":
computation = previous * current;
break;
default:
break;
}
if (isNaN(computation)) return;
currentOperand = computation;
previousOperand = "";
operation = undefined;
DisplayNumbers();
temporaryOperand = currentOperand;
currentOperand = "";
}
function AllClear() {
if (!previousOperand) {
currentOperand = currentOperand.slice(0, currentOperand.length - 1);
} else {
previousOperand = "";
currentOperand = "";
operation = undefined;
acButton.innerHTML = "C";
}
DisplayNumbers();
}
function PlusMinus() {
currentOperand = currentOperand * -1;
DisplayNumbers();
}
function Percent() {
currentOperand = currentOperand / 100;
DisplayNumbers();
}
// Add event listener to operator buttons
additionButton.addEventListener("click", () => {
ChooseOperation("+");
});
subtractionButton.addEventListener("click", () => {
ChooseOperation("-");
});
multiplicationButton.addEventListener("click", () => {
ChooseOperation("*");
});
divisionButton.addEventListener("click", () => {
ChooseOperation("÷");
});
equalsButton.addEventListener("click", () => {
Compute();
});
// Add event listener to top buttons
acButton.addEventListener("click", () => {
AllClear();
});
pmButton.addEventListener("click", () => {
PlusMinus();
});
percentButton.addEventListener("click", () => {
Percent();
});
// Add event listener to number buttons
for (let i = 0; i < numbersArray.length; i++) {
const number = numbersArray[i];
number.addEventListener("click", () => {
AppendNumber(i);
temporaryOperand = "";
});
}
decimalButton.addEventListener("click", () => {
AppendNumber(".");
});