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

Commit

Permalink
better lone parenthesis handling (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielMahan authored Apr 9, 2020
1 parent 8a8f26b commit d65a386
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/Symbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ Symbols.getLastSymbolTerm = function(node, symbolName) {
}
}
}
else if (Node.Type.isParenthesis(node)) {
return Symbols.getLastSymbolTerm(node.content, symbolName);
}

return null;
};

Expand Down
10 changes: 8 additions & 2 deletions lib/solveEquation/EquationOperations.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,16 @@ EquationOperations.removeSymbolFromRightSide = function(equation, symbolName) {
// or dividing all other symbols and constants from both sides appropriately
// TODO: support inverting functions e.g. sqrt, ^, log etc.
EquationOperations.isolateSymbolOnLeftSide = function(equation, symbolName) {
const leftNode = equation.leftNode;
let nonSymbolTerm = Symbols.getLastNonSymbolTerm(leftNode, symbolName);
let leftNode = equation.leftNode;

if (Node.Type.isParenthesis(leftNode)) {
// if entire left node is a parenthesis, we can ignore the parenthesis
leftNode = leftNode.content;
}

let nonSymbolTerm = Symbols.getLastNonSymbolTerm(leftNode, symbolName);
let inverseOp, inverseTerm, changeType;

if (!nonSymbolTerm) {
return EquationStatus.noChange(equation);
}
Expand Down
2 changes: 2 additions & 0 deletions test/solveEquation/solveEquation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ describe('solveEquation for =', function () {
['2/(1 + 1 + 4x) = 1/3', 'x = 1'],
['(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']
// 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

0 comments on commit d65a386

Please sign in to comment.