-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
229 lines (201 loc) · 6.08 KB
/
script.js
File metadata and controls
229 lines (201 loc) · 6.08 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//operate function
let num1 = 0;
let num2 = 0;
let operator;
let zerroError = false;
let displayVal = "";
let operandCount = 0; //to know when pairs are created
let largeDisplay = document.querySelector(".large-display");
let smallDisplay = document.querySelector(".small-display");
let numberBtns = document.querySelectorAll(".number");
let operatorBtns = document.querySelectorAll(".operator");
let equals = document.querySelector(".equals");
let acBtn = document.querySelector(".ac");
let decimalBtn = document.querySelector(".decimal");
function operate(num1, num2, operator) {
num1 = Number(num1);
num2 = Number(num2);
let result;
switch (operator) {
case "+":
result = add(num1, num2);
break;
case "-":
result = subtract(num1, num2);
break;
case "*":
result = multiply(num1, num2);
break;
case "/":
result = divide(num1, num2);
break;
default:
break;
}
return result;
}
const add = (num1, num2) => {
return num1 + num2;
};
const multiply = function (num1, num2) {
return num1 * num2;
};
const subtract = function (num1, num2) {
return num1 - num2;
};
const divide = function (num1, num2) {
if (num2 == 0) {
zerroError = true;
return "ERROR";
}
return num1 / num2;
};
//event listeners
numberBtns.forEach((number) => {
number.addEventListener("click", numberClick);
});
operatorBtns.forEach((operator) => {
operator.addEventListener("click", operatorClick);
});
equals.addEventListener("click", equalsHandler);
acBtn.addEventListener("click", clearFunc);
decimalBtn.addEventListener("click", decimalClick);
function numberClick() {
console.log("\nNumberclick()");
if (zerroError) {
console.log("zerroError");
return;
}
//FCC User Story #10: Prevent multiple leading zeros
if (displayVal === "0" && this.value === "0") {
return; // Don't allow multiple zeros at start
}
// If current display is "0", replace it with the new number (unless it's a decimal)
if (displayVal === "0" && this.value !== "0") {
displayVal = this.value;
} else {
displayVal = displayVal + this.value;
}
//display current pressed
largeDisplay.innerText = displayVal;
if (operandCount == 0) {
//first time number is entered
num1 = displayVal;
} else if (operandCount == 1 || operandCount == 2) {
// number clicked after an operator is clicked. ie. num1 exists
num2 = displayVal;
operandCount = 2;
}
console.log("displayval:", displayVal, " num1,num2: ", num1, num2);
console.log("operator,operandcount: ", operator, operandCount);
}
function decimalClick() {
console.log("\nDecimalclick()");
if (zerroError) {
return;
}
// FCC User Story #11: Don't allow multiple decimals in one number
if (displayVal.includes(".")) {
return;
}
// If displayVal is empty or just "0", start with "0."
if (displayVal === "" || displayVal === "0") {
displayVal = "0.";
} else {
displayVal = displayVal + ".";
}
largeDisplay.innerText = displayVal;
if (operandCount == 0) {
num1 = displayVal;
} else if (operandCount == 1 || operandCount == 2) {
num2 = displayVal;
operandCount = 2;
}
console.log("decimal - displayval:", displayVal, " num1,num2: ", num1, num2);
}
function operatorClick() {
console.log("\nOperatorclick()");
//FCC User Story #13: Handle consecutive operators
if (operandCount == 1 && displayVal === "") {
// This means an operator was just clicked - replace the previous operator
// unless it's a minus sign following another operator (for negative numbers)
let prevOperator = operator;
if (this.value === "-" && prevOperator !== "-") {
// Allow negative numbers: if previous operator wasn't minus, allow this minus
displayVal = "-";
largeDisplay.innerText = displayVal;
return;
} else {
// Replace the previous operator with this one
operator = this.value;
largeDisplay.innerText = operator;
smallDisplay.innerText = num1 + " " + operator;
return;
}
}
if (operandCount == 0) {
//for when operator clicked after a number
operandCount = 1;
} else if (operandCount == 2) {
//when two operands exists
let tempReturn = operate(num1, num2, operator);
//to check for divide by zero errors.
if (zerroError) {
console.log("zerro error");
//equalshandler to print error and show calculation on small display
equalsHandler();
return;
} else {
//set num1 as result, num2 to zero and operandcount=1 to continue operations.
num1 = tempReturn;
num2 = 0;
operandCount = 1;
}
}
//set operator val and print calc on small display and currently pressed on largedisplay
operator = this.value;
largeDisplay.innerText = operator;
smallDisplay.innerText = num1 + " " + operator;
displayVal = ""; //reset displaVal for numberClick to work properly
console.log("displayval:", displayVal, " num1,num2: ", num1, num2);
console.log("operator,operandcount: ", operator, operandCount);
}
//onclick A/C button
function clearFunc() {
//resets all values, clears small display and print 0 in largedisplay
smallDisplay.innerText = "";
largeDisplay.innerText = "0";
zerroError = false;
num1 = 0;
num2 = 0;
displayVal = "";
operator = null;
operandCount = 0;
console.log(
"cleared: num1,num2,operator,operandcount:",
num1,
num2,
operator,
operandCount
);
}
//equals
function equalsHandler() {
console.log("EqualsHandler");
//only print equals when two operands are present.
if (operandCount == 2) {
let calc = operate(num1, num2, operator);
console.log("equals; ", calc);
smallDisplay.innerText = num1 + " " + operator + " " + num2 + " = ";
largeDisplay.innerText = calc;
//check zerroError to keep zerroError working normally
if (!zerroError) {
//operandcount=0 so numberclick works normally after equals.
operandCount = 0;
//displayVal=calc so it can be used for num1 concatination on numclick
displayVal = calc.toString();
//num1=calc so operatorclick works normally after equals.
num1 = calc;
}
}
}