Skip to content

Commit 7dce0ab

Browse files
committed
better lone parenthesis handling
1 parent 8a8f26b commit 7dce0ab

3 files changed

Lines changed: 14 additions & 2 deletions

File tree

lib/Symbols.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ Symbols.getLastSymbolTerm = function(node, symbolName) {
3939
}
4040
}
4141
}
42+
else if (Node.Type.isParenthesis(node)) {
43+
return Symbols.getLastSymbolTerm(node.content, symbolName);
44+
}
45+
4246
return null;
4347
};
4448

lib/solveEquation/EquationOperations.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,16 @@ EquationOperations.removeSymbolFromRightSide = function(equation, symbolName) {
128128
// or dividing all other symbols and constants from both sides appropriately
129129
// TODO: support inverting functions e.g. sqrt, ^, log etc.
130130
EquationOperations.isolateSymbolOnLeftSide = function(equation, symbolName) {
131-
const leftNode = equation.leftNode;
132-
let nonSymbolTerm = Symbols.getLastNonSymbolTerm(leftNode, symbolName);
131+
let leftNode = equation.leftNode;
132+
133+
if (Node.Type.isParenthesis(leftNode)) {
134+
// if entire left node is a parenthesis, we can ignore the parenthesis
135+
leftNode = leftNode.content;
136+
}
133137

138+
let nonSymbolTerm = Symbols.getLastNonSymbolTerm(leftNode, symbolName);
134139
let inverseOp, inverseTerm, changeType;
140+
135141
if (!nonSymbolTerm) {
136142
return EquationStatus.noChange(equation);
137143
}

test/solveEquation/solveEquation.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ describe('solveEquation for =', function () {
9595
['2/(1 + 1 + 4x) = 1/3', 'x = 1'],
9696
['(3 + x) / (x^2 + 3) = 1', 'x = [0, 1]'],
9797
['6/x + 8/(2x) = 10', 'x = 1'],
98+
['(x+1)=4', 'x = 3'],
99+
['((x)/(4))=4', 'x = 16']
98100
// TODO: fix these cases, fail because lack of factoring support, for complex #s,
99101
// for taking the sqrt of both sides, etc
100102
// ['(x + y) (y + 2) = 0', 'y = -y'],

0 commit comments

Comments
 (0)