diff --git a/lib/solveEquation/stepThrough.js b/lib/solveEquation/stepThrough.js index 64380c72..e3ca243c 100644 --- a/lib/solveEquation/stepThrough.js +++ b/lib/solveEquation/stepThrough.js @@ -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 = []; @@ -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. diff --git a/test/solveEquation/solveEquation.test.js b/test/solveEquation/solveEquation.test.js index c92d304a..ef85d748 100644 --- a/test/solveEquation/solveEquation.test.js +++ b/test/solveEquation/solveEquation.test.js @@ -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'], @@ -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],