-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
194 lines (153 loc) · 7.9 KB
/
script.js
File metadata and controls
194 lines (153 loc) · 7.9 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
/*
OUTDATED!
Steps for trial and error:
1. Check any possible combination of numbers within a range.
2. Check whether or not the equations are true.
3. If they are not, keep trying. Store already tried values in an array, and don't try them again.
4. Once the correct values are found, display them.
*/
function solve(equationOne, equationTwo, attemptRange, variableOneName, variableTwoName) {
// loop
let foundX; // the value of X which was found
let foundY; // the value of Y which was found
let attemptCount = 0;
let expressionOne = equationOne.split("=")[0].trim();
let expressionTwo = equationTwo.split("=")[0].trim();
let expectedValueOne = parseInt(equationOne.split("=")[1].trim());
let expectedValueTwo = parseInt(equationTwo.split("=")[1].trim());
console.log(`expected ${variableOneName}: ${expectedValueOne}`);
console.log(`expected ${variableTwoName}: ${expectedValueTwo}`);
let tableData = [];
// let attemptedValues = [];
// // structure of each item in attemptedValues:
// // {x: number, y: number}
let tableEnabled = document.getElementById("table-enabled").value;
try {
// IDEA: instead of checking random numbers, check one value of x with _ values of y
for (var x = 0; x < attemptRange + 1; x++) {
let foundThisStuff = false;
for (var y = 0; y < attemptRange + 1; y++) {
if (tableEnabled) {
let tableDataPre = [x, y];
attemptCount++;
let substitutedExpressionOne = convertAlgebraMultiplicationToTerms(convertAlgebraMultiplicationToTerms(expressionOne, variableTwoName), variableOneName).replaceAll(variableOneName, x).replaceAll(variableTwoName, y);
let resultExpressionOne = math.evaluate(substitutedExpressionOne);
// console.log(`expected: ${expectedValueOne}`);
// console.log(`got: ${resultExpressionOne}`);
tableDataPre.push(resultExpressionOne);
console.log("X matches");
let substitutedExpressionTwo = convertAlgebraMultiplicationToTerms(convertAlgebraMultiplicationToTerms(expressionTwo, variableTwoName), variableOneName).replaceAll(variableOneName, x).replaceAll(variableTwoName, y);
let resultExpressionTwo = math.evaluate(substitutedExpressionTwo);
// console.log(`expected: ${expectedValueTwo}`);
// console.log(`got: ${resultExpressionTwo}`);
tableDataPre.push(resultExpressionTwo);
if (resultExpressionOne == expectedValueOne) {
if (resultExpressionTwo == expectedValueTwo) {
console.log("Y matches");
console.log("Found!");
foundX = x;
foundY = y;
foundThisStuff = true;
break;
}
}
tableData.push(tableDataPre);
if (tableData.length > 4) {
tableData.shift();
}
} else {
attemptCount++;
let substitutedExpressionOne = convertAlgebraMultiplicationToTerms(convertAlgebraMultiplicationToTerms(expressionOne, variableTwoName), variableOneName).replaceAll(variableOneName, x).replaceAll(variableTwoName, y);
let resultExpressionOne = math.evaluate(substitutedExpressionOne);
// console.log(`expected: ${expectedValueOne}`);
// console.log(`got: ${resultExpressionOne}`);
if (resultExpressionOne == expectedValueOne) {
console.log("X matches");
let substitutedExpressionTwo = convertAlgebraMultiplicationToTerms(convertAlgebraMultiplicationToTerms(expressionTwo, variableTwoName), variableOneName).replaceAll(variableOneName, x).replaceAll(variableTwoName, y);
let resultExpressionTwo = math.evaluate(substitutedExpressionTwo);
// console.log(`expected: ${expectedValueTwo}`);
// console.log(`got: ${resultExpressionTwo}`);
if (resultExpressionTwo == expectedValueTwo) {
console.log("Y matches");
console.log("Found!");
foundX = x;
foundY = y;
foundThisStuff = true;
break;
}
}
}
}
if (foundThisStuff) {
break;
}
}
} catch (e) {
console.log("Error: " + e);
console.trace();
showOutput(e);
return;
}
if (foundX && foundY) {
console.log("Successfully found X and Y after " + attemptCount + " tries!");
console.log(`X: ${foundX}`);
console.log(`Y: ${foundY}`);
showOutput(`${variableOneName} = ${foundX}\n${variableTwoName} = ${foundY}`);
} else {
console.log("Could not find result! :(");
showOutput("Could not find values :(");
}
setTimeout(() => { // so u can see it
document.getElementById("status").style.display = "none";
}, 10);
if(tableEnabled) makeResultsTable(variableOneName, variableTwoName, expressionOne, expressionTwo, tableData);
}
function convertAlgebraMultiplicationToTerms(expression, variableName) {
// Escape the variable name for regex
const escapedVariableName = variableName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
// Match digit followed by variable, not preceded by backslash or inside groups
const regex = new RegExp(`(?<!\\\\)(\\d+)(${escapedVariableName})(?![\\s]*[\\)\\\]\\}])`, 'g');
return expression.replace(regex, '$1 * $2');
}
function showOutput(text) {
document.getElementById("output").innerHTML = text;
}
function makeResultsTable(variableOneName, variableTwoName, expressionOne, expressionTwo, dataArray) {
const table = document.getElementById('table');
table.innerHTML = `
<tr>
<td>${variableOneName}</td>
<td>${variableTwoName}</td>
<td>${expressionOne}</td>
<td>${expressionTwo}</td>
</tr>
<tr>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
</tr>
`;
for (i in dataArray) {
table.innerHTML += `<tr><td>${dataArray[i][0]}</td><td>${dataArray[i][1]}</td><td>${dataArray[i][2]}</td><td>${dataArray[i][3]}</td></tr>`
}
}
function useInputsAndSolve() {
document.getElementById("status").style.display = "inline";
let equationOne = document.getElementById("equation-one").value;
let equationTwo = document.getElementById("equation-two").value;
let attemptRange = document.getElementById("attempt-range").value;
let variableOneName = document.getElementById("variable-one-name").value;
let variableTwoName = document.getElementById("variable-two-name").value;
if (!equationOne.includes(variableOneName) || !equationTwo.includes(variableOneName) || !equationOne.includes(variableTwoName) || !equationTwo.includes(variableTwoName)) {
showOutput("You didn't use your variable in one or more of the equations! \nCheck if you've set the correct variable name.");
document.getElementById("status").style.display = "none";
return;
}
if (equationOne.split("=").length < 2 || equationTwo.split("=").length < 2) {
showOutput("One or more of the equations is invalid (might be an expression instead of an equation)");
document.getElementById("status").style.display = "none";
return;
}
solve(equationOne, equationTwo, attemptRange, variableOneName, variableTwoName);
}