-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
201 lines (187 loc) · 6.07 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
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
// Functions for Operators
function addition(num1, num2) {
let answer = Number(num1) + Number(num2);
updateDisplay(answer);
}
function subtraction(num1, num2) {
let answer = Number(num1) - Number(num2);
updateDisplay(answer);
}
function multiplication(num1, num2) {
let answer = Number(num1) * Number(num2);
updateDisplay(answer);
}
function division(num1, num2) {
let answer = Number(num1) / Number(num2);
updateDisplay(answer);
}
// Update display and variables after solve
function updateDisplay(answer) {
display.textContent = answer;
reset = true;
num1 = answer;
num2 = null;
point = false;
}
// Variables
let oper;
let num1;
let num2;
let equals;
let numScreen;
let reset;
let point;
// Functions of the Calculator
function operate(oper, num1, num2) {
switch (oper) {
case '+':
addition(num1, num2);
break;
case '-':
subtraction(num1, num2);
break;
case `*`:
multiplication(num1, num2);
break;
case '/':
division(num1, num2);
break;
default:
alert("Please input a valid operator!");
}
}
function disableOper() {
buttons.forEach((btn) => {
const btnValue = btn.getAttribute('value');
for (const value in operArr) {
if (btnValue == operArr[value]) {
btn.setAttribute('disabled', '');
}
}
})
};
function enableOper() {
buttons.forEach((btn) => {
const btnValue = btn.getAttribute('value');
for (const value in operArr) {
if (btnValue == operArr[value]) {
btn.removeAttribute('disabled');
}
}
})
};
const buttons = document.querySelectorAll('button');
const display = document.querySelector('#mainScreen');
const miniDisplay = document.querySelector('#holdScreen');
// Add numbers to the screen
const numArr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
const operArr = ['/', '*', '-', '+'];
buttons.forEach((btn) => {
const btnValue = btn.getAttribute('value');
btn.addEventListener('click', () => {
for (const value in numArr) {
if (btnValue == value) {
switch (true) {
case (num1 !== null):
enableOper();
case (num1 == null):
switch (reset) {
case (true):
display.textContent = 0;
reset = false;
default:
if (display.textContent == '0') {
display.textContent = btnValue;
break;
} else {
display.append(btnValue);
break;
};
};
}
}
};
for (const value in operArr) {
if (btnValue == operArr[value]) {
if (oper == null) {
num1 = display.textContent;
oper = btnValue;
miniDisplay.textContent = display.textContent + " " + oper;
display.textContent = 0;
disableOper();
} else {
num2 = display.textContent;
miniDisplay.textContent = miniDisplay.textContent + " " + display.textContent + " " + oper;
operate(oper, num1, num2);
oper = btnValue;
disableOper();
}
}
};
if (btnValue == '=') {
if (oper == null) {
alert ('Please choose a valid operator!');
} else if (display.textContent == 0) {
alert("\nTo Infinity and Beyond!\n\nNice try, Space Pirate. You know we can't divide by 0!");
} else {
num2 = display.textContent;
miniDisplay.textContent = miniDisplay.textContent + " " + display.textContent;
display.textContent = 0;
operate(oper, num1, num2);
oper = null;
}
} else if (btnValue == '<-') {
if (display.textContent == 0) {
return;
} else {
display.textContent = display.textContent.replace(display.textContent.charAt(display.textContent.length - 1), "");
if (display.textContent == 0) {
display.textContent = 0;
} else {
return;
};
};
} else if (btnValue == 'C') {
num1 = null;
num2 = null;
oper = null;
reset = false;
point = false;
enableOper();
miniDisplay.textContent = "";
display.textContent = 0;
} else if (btnValue == '.') {
switch (reset) {
case (true):
display.textContent = 0;
reset = false;
default:
switch (point) {
case (true):
break;
default:
if (display.textContent == '0') {
display.textContent = 0 + btnValue;
break;
} else {
display.append(btnValue);
break;
};
}
};
point = true;
}
// Use to cleanup code later
// if (btnValue in numArr) {
// if (display.textContent == 0) {
// display.textContent = btnValue;
// } else {
// display.append(btnValue);
// }
// } else if (btnValue == '=') {
// operate(oper, num1, num2);
// } else {
// console.log('What da hail')
// };
});
});