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

Better lone parenthesis handling #250

Merged
merged 1 commit into from
Apr 9, 2020
Merged
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
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