forked from Emmet-Allen/quickCalcExtension
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickCalc.js
More file actions
273 lines (240 loc) · 7.06 KB
/
Copy pathquickCalc.js
File metadata and controls
273 lines (240 loc) · 7.06 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//Number Inputs
let firstNumber = document.getElementById("firstNumber");
let secondNumber = document.getElementById("secondNumber");
let thirdNumber = document.getElementById("thirdNumber");
// Calculate Button
const calculate = document.getElementById("Calculate");
// Math Formula Named Radio Buttons
let subtract = document.getElementById("subtraction");
let addition = document.getElementById("addition");
let division = document.getElementById("division");
let multiplication = document.getElementById("multiplication");
let factorial = document.getElementById("factorial");
let permutation = document.getElementById("permutations");
let kPermutation = document.getElementById("kPermutations");
let greatestCD = document.getElementById("GCD");
let arithmeticSeries = document.getElementById("ArithmeticSeries");
let arithmeticSequence = document.getElementById("ArithmeticSequence");
let euclidTriple = document.getElementById("EuclidsTriple");
//Input Boxes
let secondInput = document.getElementById("secondNumber");
let thirdInput = document.getElementById("thirdNumber");
//Solution History List
let historyList = [];
//Hide input via async function
addition.addEventListener("click", async () => {
thirdNumber.style.visibility = "hidden";
secondInput.style.visibility = "visible";
});
subtract.addEventListener("click", async () => {
thirdNumber.style.visibility = "hidden";
secondInput.style.visibility = "visible";
});
multiplication.addEventListener("click", async () => {
thirdNumber.style.visibility = "hidden";
secondInput.style.visibility = "visible";
});
division.addEventListener("click", async () => {
thirdNumber.style.visibility = "hidden";
secondInput.style.visibility = "visible";
});
factorial.addEventListener("click", async () => {
secondInput.style.visibility = "hidden";
thirdInput.style.visibility = "hidden";
});
permutation.addEventListener("click", async () => {
thirdNumber.style.visibility = "hidden";
secondInput.style.visibility = "visible";
});
kPermutation.addEventListener("click", async () => {
secondInput.style.visibility = "visible";
thirdInput.style.visibility = "hidden";
});
greatestCD.addEventListener("click", async () => {
secondInput.style.visibility = "visible";
thirdInput.style.visibility = "hidden";
});
arithmeticSeries.addEventListener("click", async () => {
secondInput.style.visibility = "visible";
thirdInput.style.visibility = "visible";
});
arithmeticSequence.addEventListener("click", async () => {
secondInput.style.visibility = "visible";
thirdInput.style.visibility = "visible";
});
euclidTriple.addEventListener("click", async () => {
thirdNumber.style.visibility = "hidden";
secondInput.style.visibility = "visible";
});
//Event Listener To Calculate Formula with given inputs
calculate.addEventListener("click", operation);
// calculate.addEventListener("click", displayHistory);
// Checks which Formula was choosen and performs calculation
async function operation() {
let solution = null;
if (addition.checked) {
solution = addNumbers();
}
if (subtract.checked) {
solution = subNumbers();
}
if (multiplication.checked) {
solution = multiNumbers();
}
if (division.checked) {
solution = divNumbers();
}
if (factorial.checked) {
solution = factorialFunc(parseInt(firstNumber.value));
}
if (permutation.checked) {
solution = permutationFunc(
parseInt(firstNumber.value),
parseInt(secondNumber.value)
);
}
if (kPermutation.checked) {
solution = kPermutationNumbers(
parseInt(firstNumber.value),
parseInt(secondNumber.value)
);
}
if (greatestCD.checked) {
solution = gcdFunc(
parseInt(firstNumber.value),
parseInt(secondNumber.value)
);
}
if (arithmeticSeries.checked) {
solution = arithmeticSeriesFunc(
parseInt(firstNumber.value),
parseInt(secondNumber.value),
parseInt(thirdNumber.value)
);
}
if (arithmeticSequence.checked) {
solution = arithmeticSeqFunc(
parseInt(firstNumber.value),
parseInt(secondNumber.value),
parseInt(thirdNumber.value)
);
}
if (euclidTriple.checked) {
solution = euclidTripleFunc(
parseInt(firstNumber.value),
parseInt(secondNumber.value)
);
}
console.log(solution);
if ((solution != null && !isNaN(solution)) || Array.isArray(solution)) {
solution = "[" + solution + "]";
historyList.unshift(solution);
displayHistory(historyList);
}
return displayResult(solution);
}
// Functions for formulas
function addNumbers() {
let solution = parseInt(firstNumber.value) + parseInt(secondNumber.value);
return solution;
}
function subNumbers() {
let solution = parseInt(firstNumber.value) - parseInt(secondNumber.value);
return solution;
}
function multiNumbers() {
let solution = parseInt(firstNumber.value) * parseInt(secondNumber.value);
return solution;
}
function divNumbers() {
let solution = parseInt(firstNumber.value) / parseInt(secondNumber.value);
return solution;
}
function factorialFunc(value) {
if (isNaN(value)) {
return value;
}
let solution = 1;
let number = value;
while (number > 0) {
solution *= number;
number--;
}
return solution;
}
function permutationFunc(valueA, valueB) {
let solution = Math.pow(valueA, valueB);
return solution;
}
function kPermutationNumbers(valueN, valueK) {
let top = factorialFunc(valueN);
let bottom = factorialFunc(valueN - valueK);
let solution = top / bottom;
return solution;
}
function gcdFunc(valueA, valueB) {
let a = valueA;
let b = valueB;
if (b == 0) {
var solution = a;
}
if (a == 0) {
var solution = b;
} else {
while (b > 0) {
let remainder = valueA % valueB;
a = b;
b = remainder;
}
var solution = a;
}
return solution;
}
function arithmeticSeriesFunc(valueA, valueB, valueN) {
let seriesNum = valueB - valueA;
let seriesCount = valueN - 2;
let start = valueB;
while (seriesCount > 0) {
start += seriesNum;
seriesCount--;
}
let solution = start;
return solution;
}
function arithmeticSeqFunc(valueA, valueB, valueN) {
let top = valueN * (valueA + valueB);
let solution = top / 2;
return solution;
}
function euclidTripleFunc(valueM, valueN) {
if (isNaN(valueM) || isNaN(valueN)) {
return NaN;
}
let solution = [];
let a = valueM * valueN;
let b = (Math.pow(valueM, 2) - Math.pow(valueN, 2)) / 2;
let c = (Math.pow(valueM, 2) + Math.pow(valueN, 2)) / 2;
solution.push(a, b, c);
return solution;
}
// Displays result
function displayResult(solution) {
let result = "Result is";
firstNumber.value = "";
secondNumber.value = "";
thirdNumber.value = "";
if (isNaN(solution) && !Array.isArray(solution)) {
result = "";
solution = "Please Enter Valid Input(s)";
}
if (solution == null && !Array.isArray(solution)) {
result = "";
solution = "Please Select A Formula";
}
return (document.getElementById("ans").textContent =
result + " " + "[" + solution + "]");
}
// List to store history
function displayHistory(historyList) {
return (document.getElementById("history").textContent = historyList);
}