-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjava.js
292 lines (270 loc) · 8.03 KB
/
java.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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Global declaration of common variables
let equation = [];
let length = 0;
let display = "";
let error = false;
function character(value) {
// Sound effect when pressed
let sound = new Audio('Assets/ClickSFX.wav');
sound.play();
// If less than character limit start displaying equation and appending to equation.
if (document.getElementById("array").innerHTML.length + (value.toString()).length <= 17) {
display = "";
// returns local storage if Ans
if (value == 'Ans') {
//seperates length of Ans into the array
for (i = 0; i < localStorage.getItem("prev").length; i++) {
previous = Array.from(String(localStorage.getItem("prev")), String);
equation[length] = previous[i];
console.log('equation is: ' + equation);
length++;
}
}
else {
equation[length] = value;
length++;
}
// displays all characters of array and converts operators to common symbols
for (i = 0; i < length; i++) {
if (equation[i] == '*') {
display = display + 'x';
}
else if (equation[i] == '/' ) {
display = display + '÷';
}
else {
display = display + equation[i];
}
}
document.getElementById("array").innerHTML = display;
}
}
// clears each character of the array when screen is cleared
function reset() {
let sound = new Audio('Assets/ClickSFX.wav');
sound.play();
for (i = 0; i < length; i++) {
equation.pop()
}
length = 0;
display = "";
document.getElementById("array").innerHTML = "";
document.getElementById("answer").innerHTML = "";
}
// removes last character from array
function remove() {
let sound = new Audio('Assets/ClickSFX.wav');
sound.play();
display = "";
equation.pop()
length = length - 1;
for (i = 0; i < length; i++) {
if (equation[i] == '*') {
display = display + 'x';
}
else if (equation[i] == '/' ) {
display = display + '÷';
}
else {
display = display + equation[i];
}
}
document.getElementById("array").innerHTML = display;
}
//returns final answer
function equals() {
let sound = new Audio('Assets/ClickSFX.wav');
sound.play();
let string = "";
let newEquation = [];
let part = 0;
let result = "";
// checks to see if the decimal place is used correctly
for (i = 0; i < equation.length; i++) {
if (equation[i] == '.') {
if (equation[i + 1] == '/' || equation[i + 1] == '*' || equation[i + 1] == '-' || equation[i + 1] == '+') {
error = true;
}
for(j = i; j < equation.length - i; j++) {
if (equation[j] == '*' || equation[j] == '/' || equation[j] == '+' || equation[j] == '-' || j == equation.length) {
break;
}
else {
if (equation[j + 1] == '.') {
error = true;
break;
}
}
}
}
}
// joins all integers together into one string
for (i = 0; i < length; i++) {
if (equation[i] != '*' && equation[i] != '-' && equation[i] != '/' && equation[i] != '+') {
string = string + equation[i];
}
// adds string to the new equation, adds operator and begins new element
else {
if (string != "") {
newEquation[part] = string;
part = part + 1;
}
string = "";
newEquation[part] = equation[i];
part = part + 1;
}
}
// conditional to check if last character is operator and first character is not a multiply or divide
if (string != "" && newEquation[0] != '*' && newEquation[0] != '/') {
newEquation[part] = string;
}
else {
error = true;
}
console.log("error: " + error);
// Do heavy calculations
if (equation != []) {
let equation = clean(newEquation);
result = calculate(equation);
}
// if equation is not valid:
if (error == true && equation != []) {
result = "error";
}
//equation clear, return clear
else if (equation == []) {
result = "";
}
if ((result === "NaN" || result === "Infinity") && error == false) {
result = "Naughty Naughty";
document.getElementById("answer").style.color = "#FF0000";
}
console.log("error? " + error);
console.log("answer?" + result);
//set local item to be accessed by Ans later
if (error == false && equation != []) {
localStorage.setItem("prev", result);
}
document.getElementById("answer").innerHTML = result;
error = false;
}
//simplifies operators
function clean(equation) {
for (i = 0; i < equation.length; i++) {
console.log("equation length is:" + equation.length);
// turns '++' to '+' and '+-' to '-'
if (equation[i] == '+') {
while (equation[i + 1] == '+' && equation[i] == '+') {
equation.splice(i + 1, 1);
i = i - 1;
}
while (equation[i + 1] == '-' && equation[i] == '+') {
equation.splice(i + 1, 1);
equation[i] = '-'
i = i - 1;
}
}
// turns '-+' to '-' and '--' to '+'
if (equation[i] == '-') {
while (equation[i + 1] == '+' && equation[i] == '-') {
equation.splice(i + 1, 1);
i = i - 1;
}
while (equation[i + 1] == '-' && equation[i] == '-') {
equation.splice(i + 1, 1);
equation[i] = '+'
i = i - 1;
}
}
//////////////////////////////////////////// here
if (equation[i] == '*' && (equation[i + 1] == '*' || equation[i + 1] == '/')) {
error = true;
console.log("please work");
}
if (equation[i] == '/' && (equation[i + 1] == '*' || equation[i + 1] == '/')) {
error = true;
}
}
return equation;
}
//Function that actually does the maths
function calculate(equation) {
let calculation = []
let value = 0;
cleaned = compress(equation);
// BODMAS
for (i = 0; i < cleaned.length; i++) {
// Multiply Numbers
if (cleaned[i] == '*') {
value = parseFloat(cleaned[i - 1]) * parseFloat(cleaned[i + 1]);
cleaned[i - 1] = value.toString();
cleaned.splice(i, 2);
i = i - 2;
}
//divide numbers
if (cleaned[i] == '/') {
value = parseFloat(cleaned[i - 1]) / parseFloat(cleaned[i+1]);
cleaned[i-1] = value.toString();
cleaned.splice(i, 2);
i = i - 2;
}
}
//Removes '+' from start of equation
if (cleaned[0] == '+') {
cleaned.splice(0, 1);
}
// Adds numbers together (negative numbers will naturally subtract from this)
if (cleaned.length > 1 && cleaned[0] != '+' && cleaned[0] != '-') {
for (i = 0; i < cleaned.length - 1; i++) {
let num = parseFloat(cleaned[i]) + parseFloat(cleaned[i + 1]);
cleaned[i] = num.toString();
cleaned.splice(i + 1, 1);
i--;
}
}
//Creates final equation
for (i = 0; i < cleaned.length; i++) {
let value = cleaned[i];
calculation = calculation + value;
}
return calculation;
}
// Join operators with numbers. E.g. '-' and '45' to '-45' so '/-45' does not return NaN
function compress(cleaned) {
for (i = 0; i < cleaned.length; i++) {
// Mega compress of multiple negative numbers
if (cleaned[i + 1] == '-' && cleaned[i - 2] == '-') {
cleaned[i + 1] = "-" + cleaned[i + 2];
cleaned[i - 1] = "-" + cleaned[i - 1];
cleaned.splice(i + 2, 1);
cleaned.splice(i - 2, 1);
}
// small compress of a negative number
else if (cleaned[i + 1] == '-') {
cleaned[i + 1] = "-" + cleaned[i + 2];
cleaned.splice(i + 2, 1);
}
// small compress of a negative number
else if (cleaned[i - 2] == '-') {
cleaned[i - 1] = "-" + cleaned[i - 1];
cleaned.splice(i - 2, 1);
}
// Mega compress of multiple positive numbers
else if (cleaned[i + 1] == '+' && cleaned[i - 2] == '+') {
cleaned[i + 1] = cleaned[i + 2];
cleaned.splice(i + 2, 1);
cleaned.splice(i - 2, 1);
}
// small compress of a positive number
else if (cleaned[i + 1] == '+') {
cleaned[i + 1] = cleaned[i + 2];
cleaned.splice(i + 2, 1);
}
// small compress of a positive number
else if (cleaned[i - 2] == '+') {
cleaned[i - 1] = cleaned[i - 2];
cleaned.splice(i - 2, 1);
}
}
return cleaned;
}