-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
217 lines (177 loc) · 5.1 KB
/
scripts.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Globals
let result = null;
let firstNumber = null;
let secondNumber = null;
let savedOperator = null;
let activeOperator = null;
let displayValue = 0;
let startNewDisplay = false;
let needNewSecondNumber = false;
// Query Selectors
let display = document.querySelector("#displayText");
const clear = document.querySelector("#clear");
const deleteNum = document.querySelector("#delete");
const equalSign = document.querySelector("#equalSign");
const numberNodes = document.querySelectorAll(".number");
const operatorNodes = document.querySelectorAll(".operator");
// Event Listeners
clear.addEventListener('click', () => clearDisplay())
deleteNum.addEventListener('click', () => deleteNumber())
equalSign.addEventListener('click', () => evaluate())
numberNodes.forEach(element => {
element.addEventListener('click', () => appendNumber(element))
});
operatorNodes.forEach(element => {
element.addEventListener('click', () => {saveOperator(element); } ) // use this to add multiple listeners to one event
})
function add(a,b)
{
result = a + b;
return result;
}
function subtract(a,b)
{
result = a - b;
return result;
}
function multiply(a,b)
{
result = a * b;
return result;
}
function divide(a,b)
{
if(b == 0)
{
window.alert("Nope, You can't do that!");
secondNumber = 1;
activeOperator = "x"
result = operate(activeOperator,a,secondNumber);
displayValue = 1; // this somehow fixed it
return result;
}
result = a / b;
return result;
}
function operate(operator, a, b)
{
const A = Number(a);
const B = Number(b);
switch(operator){
case "+":
return add(A,B);
case "-":
return subtract(A,B);
case "x":
return multiply(A,B);
case "÷":
return divide(A,B);
}
}
function clearDisplay()
{
console.log("clearDisplay");
display.textContent = 0;
displayValue = display.textContent;
clearAll();
console.log("displayValue: ", displayValue);
}
function clearAll()
{
console.log("clearAll");
savedOperator = null;
activeOperator = null;
firstNumber = null;
secondNumber = null;
result = null;
}
function deleteNumber()
{
console.log("deleteNumber");
let string = displayText.textContent;
display.textContent = string.slice(0,-1);
displayValue = display.textContent;
console.log("displayValue: ", displayValue);
}
function evaluate()
{
console.log("evaluate");
if(secondNumber == null)
{
secondNumber = displayValue;
console.log("120secondNumber saved: ", secondNumber);
}
if(needNewSecondNumber)
{
secondNumber = displayValue;
console.log("secondNumber saved: ", secondNumber);
needNewSecondNumber = false;
}
operate(activeOperator,firstNumber,secondNumber);
displayResult();
}
function appendNumber(element)
{
console.log("appendNumber");
if(startNewDisplay)
{
display.textContent = element.getAttribute('value');
startNewDisplay = false;
displayValue = display.textContent;
console.log("displayValue: ", displayValue);
return;
}
if (element.getAttribute('value') == "." && display.textContent.includes(".")) // stops from user adding too many decimals
{
return;
}
if(displayValue == 0) // if the first number on the calculator display is 0 then the display should change to the number pressed
{
display.textContent = element.getAttribute('value');
}
else
display.textContent += element.getAttribute('value'); // else append the number to the end of the current number
displayValue = display.textContent;
console.log("displayValue: ", displayValue);
}
function saveOperator(element)
{
console.log("saveOperator");
savedOperator = element.getAttribute('value');
console.log("savedOperator: ", savedOperator);
if(firstNumber == null && activeOperator == null)
{
firstNumber = displayValue;
startNewDisplay = true;
console.log("firstNumber saved: ", firstNumber);
activeOperator = savedOperator; // operator to be used in operate()
console.log("activeOperator: ", activeOperator);
}
else if(firstNumber != null)
{
secondNumber = displayValue;
console.log("secondNumber saved: ", secondNumber);
operate(activeOperator,firstNumber,secondNumber);
displayResult();
startNewDisplay = true;
firstNumber = result;
activeOperator = savedOperator;
console.log("new active operator: ", activeOperator);
console.log("new first number: ", firstNumber);
console.log("need new second number...");
needNewSecondNumber = true;
}
else if(firstNumber != null && needNewSecondNumber)
{
secondNumber = displayValue;
console.log("new secondNumber saved: ", secondNumber);
operate(activeOperator, firstNumber, secondNumber);
displayResult();
}
}
function displayResult()
{
console.log("displayResult");
console.log("result: ", result);
display.textContent = result;
}