-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
84 lines (77 loc) · 2.68 KB
/
index.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
//Get all necessary elements
const rowEchelonValues = Array.from(document.querySelectorAll('.row-echelon-value'));
const solutionContainer = document.querySelector('#solution-container');
const unSolvableMessage = document.querySelector('#unsolvable-message');
const xLabel = document.querySelector('#x');
const yLabel = document.querySelector('#y');
const zLabel = document.querySelector('#z');
const modeButton = document.querySelector('#color-mode');
const body = document.querySelector('body');
const inputMatrix = [];
for (let i = 0; i < inputs.length; i += 4) {
inputMatrix.push(inputs.slice(i, i + 4));
}
const outputMatrix = [];
for (let i = 0; i < rowEchelonValues.length; i += 4) {
outputMatrix.push(rowEchelonValues.slice(i, i + 4));
}
const matrix = [
[Fraction(0), Fraction(0), Fraction(0), Fraction(0)],
[Fraction(0), Fraction(0), Fraction(0), Fraction(0)],
[Fraction(0), Fraction(0), Fraction(0), Fraction(0)]
];
let rowEchelon;
let isSolved = false;
//Loop through all inputs and add corresponding event listeners
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 4; j++) {
let currentInput = inputMatrix[i][j];
currentInput.addEventListener('input', () => {
//Delete all invalid characters
currentInput.value = currentInput.value.replace(/[^0-9/.-]/g, '');
//Default value of 0
let value = Fraction(0);
if (currentInput.value !== '') {
try {
value = Fraction(currentInput.value);
} catch { }
}
matrix[i][j] = value;
rowEchelon = toRowEchelon(matrix);
updateResult();
})
}
}
function updateResult() {
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 4; j++) {
outputMatrix[i][j].textContent = rowEchelon[i][j].toString();
}
}
if (isSolvable(rowEchelon)){
let answers = solve(rowEchelon);
xLabel.textContent = `x = ${answers[0]}`;
yLabel.textContent = `y = ${answers[1]}`;
zLabel.textContent = `z = ${answers[2]}`;
solutionContainer.style.display = 'flex';
unSolvableMessage.style.display = 'none';
}
else{
solutionContainer.style.display = 'none';
unSolvableMessage.style.display = 'block';
}
}
let isDarkMode = true;
modeButton.addEventListener('click', ()=>{
isDarkMode = !isDarkMode;
if (isDarkMode){
modeButton.textContent = 'Light Mode';
} else{
modeButton.textContent = 'Dark Mode'
}
body.classList.toggle('dark-mode');
modeButton.classList.toggle('dark-mode');
inputs.forEach((input)=>{
input.classList.toggle('dark-mode');
});
})