-
Notifications
You must be signed in to change notification settings - Fork 0
/
app1.js
285 lines (261 loc) · 9.25 KB
/
app1.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
let currentApp = 1;
let onAnimation = false;
let stopErasing = false;
const equationArray = [];
document.addEventListener('keydown', function (event) {
if (event.keyCode == 13 && event.target.nodeName == 'INPUT') {
if (currentApp == 1) {
addEquation();
} else {
let form = event.target.form;
let index = Array.prototype.indexOf.call(form, event.target);
form.elements[index + 1].focus();
event.preventDefault();
}
}
});
document.querySelectorAll(".app2").forEach(item => {
item.className += " hide";
});
for (let i = 1; i < 3; i ++) {
document.getElementById(`appButton${i}`).addEventListener("click", function() {
changeApps(i);
});
}
const DOMtitle = document.querySelector(".title");
const buttonAdd = document.querySelector(".add-equation");
const buttonCreateMatrix = document.querySelector(".create-matrix");
const equationInput = document.querySelector(".equation-input");
const equationList = document.querySelector(".equation-list");
const matrixList = document.querySelector(".results-list");
buttonAdd.addEventListener("click", addEquation);
buttonCreateMatrix.addEventListener("click", addMatrix);
function addEquation() {
const newEquation = checkEquation(equationInput.value.toLowerCase());
if (newEquation == "erro") {
alert('Invalid equation!');
} else {
newEquation.orderVariables();
equationArray.push(newEquation);
const equationDiv = document.createElement("div");
equationDiv.className = "equation-div";
const equationToAdd = document.createElement("div");
equationToAdd.innerText = equationInput.value;
equationToAdd.className = "equation";
const deleteIcon = document.createElement("i");
deleteIcon.className = "fa-regular fa-trash-can";
const deleteButton = document.createElement("span");
deleteButton.className = "icon";
deleteButton.addEventListener("click", function() {
equationArray.splice(equationArray.indexOf(newEquation), 1);
equationList.removeChild(equationDiv);
checkDisabledButton();
checkInstructions();
});
checkInstructions();
deleteButton.appendChild(deleteIcon);
equationDiv.appendChild(equationToAdd);
equationDiv.appendChild(deleteButton);
equationList.appendChild(equationDiv);
checkDisabledButton();
equationInput.value = "";
}
}
function checkEquation(rawEquation) {
if (rawEquation.match(/=/g) == null) {
return "erro";
}
rawEquation = rawEquation.replaceAll(',', '.');
const equation = rawEquation.split(/[\s\+\-=]+/);
const equationSignals = rawEquation.split(/[\d\w\s=.]+/);
console.log(equation, equationSignals);
for (let i = 0; i < equation.length; i++) {
if (i !== equation.length - 1) {
if (equation[i].match(/\d+/g) == null) {
equation[i] = `1${equation[i]}`
}
if (equation[i].match(/\D/g) == null) {
return "erro";
}
}
if (equationSignals[i] == "-") {
const numToReplace = equation[i].split("");
numToReplace.unshift("-");
equation[i] = numToReplace.join("");
}
}
return new Equation(equation);
}
function addMatrix() {
const mainMatrix = createMatrix();
mainMatrix.getAllDeterminants();
printResults(mainMatrix);
}
function createMatrix() {
const variableOrder = getVariableOrder(equationArray);
const matrixArr = [];
const resultsArr = [];
let equationToPush = [];
for (let i = 0; i < equationArray.length; i++) {
equationToPush = [];
for (let j = 0; j < variableOrder.length; j++) {
if (equationArray[i][variableOrder[j]] == undefined) {
equationToPush.push(0);
} else {
equationToPush.push(equationArray[i][variableOrder[j]]);
}
}
resultsArr.push(Number(equationArray[i].results));
matrixArr.push(equationToPush);
}
return new Matrix(matrixArr, resultsArr, getVariableOrder(equationArray));
}
function getVariableOrder(equations) {
const variableOrder = [];
for (let i = 0; i < equations.length; i++) {
for (let j = 0; j < equations[i].letterList.length; j++) {
if (variableOrder.includes(equations[i].letterList[j]) == false) {
variableOrder.push(equations[i].letterList[j]);
}
}
}
return variableOrder;
}
function checkDisabledButton() {
const variableOrder = getVariableOrder(equationArray);
if (variableOrder.length == equationArray.length && variableOrder.length > 0) {
buttonCreateMatrix.disabled = false;
} else {
buttonCreateMatrix.disabled = true;
}
}
function checkInstructions() {
const instructions = document.querySelector('.instructions');
instructions.className = equationArray.length > 0 ? "instructions hide" : "instructions";
}
function printResults(matrix) {
const variableOrder = getVariableOrder(equationArray);
const matrixDiv = document.createElement("div");
matrixDiv.className = "bigger-div";
const equationsDiv = document.createElement("div");
equationsDiv.className = "equations-div";
const resultsDiv = document.createElement("div");
resultsDiv.className = "results-div";
const newEquations = document.querySelectorAll(".equation");
const hr = document.createElement("hr");
const verticalHr = document.createElement("div");
verticalHr.className = "vertical-hr has-background-dark";
newEquations.forEach(equ => {
let newEqu = document.createElement("div");
newEqu.innerText = equ.innerText;
newEqu.className = "bottom-equation";
equationsDiv.appendChild(newEqu);
});
let matrixType = "SPD";
for (let i = 0; i < variableOrder.length; i++) {
let newResultValue = matrix.determinantList[variableOrder[i]] / matrix.determinantList.matrixDeterminant;
let newResultDiv = document.createElement("div");
let newResult = document.createElement("span");
let newResultVariable = document.createElement("span");
newResultVariable.className = "result-variable has-text-primary";
newResultVariable.innerText = `${variableOrder[i]}`;
newResult.innerText = ` = ${parseFloat(newResultValue.toFixed(2))}`;
if (isNaN(newResultValue)) {
if (matrixType != "Inconsistent equation system") {
matrixType = "Dependent equation system";
}
} else if (newResultValue == "Infinity" || newResultValue == "-Infinity") {
matrixType = "Inconsistent equation system";
} else {
newResultDiv.appendChild(newResultVariable);
newResultDiv.appendChild(newResult);
resultsDiv.appendChild(newResultDiv);
}
}
if (matrixType != "SPD") {
const matrixTypeWarning = document.createElement("div");
matrixTypeWarning.innerText = matrixType;
resultsDiv.appendChild(matrixTypeWarning);
}
const deleteResultsButton = document.createElement("span");
deleteResultsButton.className = "icon is-small has-text-danger delete-results";
const buttonIcon = document.createElement("i");
buttonIcon.className = "fa-solid fa-xmark";
deleteResultsButton.addEventListener("click", function() {
matrixList.removeChild(matrixDiv);
matrixList.removeChild(hr);
});
deleteResultsButton.appendChild(buttonIcon);
matrixDiv.appendChild(equationsDiv);
//matrixDiv.appendChild(verticalHr);
matrixDiv.appendChild(resultsDiv);
matrixDiv.appendChild(deleteResultsButton);
matrixList.appendChild(matrixDiv);
matrixList.appendChild(hr);
}
function changeApps(appNumber) {
if (appNumber !== currentApp && onAnimation == false) {
onAnimation = true;
changeOutline(appNumber);
const itemsToHide = document.querySelectorAll(`.app${currentApp}`);
itemsToHide.forEach(item => {
item.className += " hide";
});
const itemsToShow = document.querySelectorAll(`.app${appNumber}`);
itemsToShow.forEach(item => {
item.className = item.className.slice(0, -5);
})
currentApp = appNumber;
changeTitle();
}
}
function changeTitle() {
let newTitle;
let oldTitle = DOMtitle.innerText;
if (currentApp == 1) {
newTitle = "Equation systems";
} else {
newTitle = "Matrices";
}
eraseTitle(oldTitle, 0);
setTimeout(function() {
if (DOMtitle.innerText.length != 0) {
stopErasing = true;
DOMtitle.innerText = "";
};
typeTitle(newTitle, 0);
} , (oldTitle.length * 30));
setTimeout(function() {
onAnimation = false
}, (oldTitle.length + newTitle.length) * 30)
}
function eraseTitle(oldTitle, count) {
if (count < oldTitle.length) {
setTimeout(function() {
if (stopErasing == true) {
stopErasing = false;
return;
}
DOMtitle.innerText = DOMtitle.innerText.slice(0, -1);
count++
eraseTitle(oldTitle, count);
}, 15);
}
}
function typeTitle(title, count) {
if (count < title.length) {
setTimeout(function() {
DOMtitle.innerText += title[count];
count++;
typeTitle(title, count);
}, 20);
}
}
function changeOutline(appNumber) {
document.getElementById(`appButton${currentApp}`).name += "-outline";
let nameToErase = document.getElementById(`appButton${appNumber}`).name;
document.getElementById(`appButton${appNumber}`).name = nameToErase.slice(0, nameToErase.length-8);
document.querySelector(`.button-app${appNumber}`).className += " has-text-primary";
let classNameToChange = document.querySelector(`.button-app${currentApp}`).className;
document.querySelector(`.button-app${currentApp}`).className = classNameToChange.slice(0, classNameToChange.length-16);
}