Skip to content
This repository was archived by the owner on Aug 29, 2024. It is now read-only.

Add support for second variable canceling out #254

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/solveEquation/stepThrough.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ function stepThrough(leftNode, rightNode, comparator, debug=false) {
if (symbolSet.size === 0) {
return solveConstantEquation(equation, debug);
}
const symbolName = symbolSet.values().next().value;

let symbolName = symbolSet.values().next().value;
let equationStatus;
let steps = [];

Expand Down Expand Up @@ -80,8 +79,13 @@ function stepThrough(leftNode, rightNode, comparator, debug=false) {
equation.rightNode = flattenOperands(equation.rightNode);

// at this point, the symbols might have cancelled out.
if (Symbols.getSymbolsInEquation(equation).size === 0) {
const postSimplificationSymbols = Symbols.getSymbolsInEquation(equation)
if (postSimplificationSymbols.size === 0) {
// if all symbols are canceled out, the equation can now be treated as a constant equation
return solveConstantEquation(equation, debug, steps);
} else if (!postSimplificationSymbols.has(symbolName)) {
// if the original symbol is canceled out but other sybols remain, reset the target symbol
symbolName = postSimplificationSymbols.values().next().value;
}

// The left side of the equation is either factored or simplified.
Expand Down
5 changes: 4 additions & 1 deletion test/solveEquation/solveEquation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ describe('solveEquation for =', function () {
['(3 + x) / (x^2 + 3) = 1', 'x = [0, 1]'],
['6/x + 8/(2x) = 10', 'x = 1'],
['(x+1)=4', 'x = 3'],
['((x)/(4))=4', 'x = 16']
['((x)/(4))=4', 'x = 16'],
['x+y=x+y', '0 = 0'],
['y + 2x = 14 + y', 'x = 7']
// TODO: fix these cases, fail because lack of factoring support, for complex #s,
// for taking the sqrt of both sides, etc
// ['(x + y) (y + 2) = 0', 'y = -y'],
Expand Down Expand Up @@ -155,6 +157,7 @@ describe('constant comparison support', function () {
['1 <= 1', ChangeTypes.STATEMENT_IS_TRUE],
['2 <= 1', ChangeTypes.STATEMENT_IS_FALSE],
['1 <= 2', ChangeTypes.STATEMENT_IS_TRUE],
['0 = 0', ChangeTypes.STATEMENT_IS_TRUE],
['( 1) = ( 14)', ChangeTypes.STATEMENT_IS_FALSE],
// TODO: when we support fancy exponent and sqrt things
// ['(1/64)^(-5/6) = 32', ChangeTypes.STATEMENT_IS_TRUE],
Expand Down