This repository was archived by the owner on Aug 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 282
Expand file tree
/
Copy pathcanFindDenominatorInNumerator.js
More file actions
98 lines (94 loc) · 3.37 KB
/
canFindDenominatorInNumerator.js
File metadata and controls
98 lines (94 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const Node = require('../node');
// Returns true if by adding a term you can simplify part of the function into
// an integer
// e.g. (2x+1)/(2x+3) -> True because of the following simplification
// (2x+1)/(2x+3) -> (2x + 3)/(2x + 3) - 2/(2x + 3) -> 1 - 2/(2x + 3)
// e.g. (2x+1)/(2x^2 + 3) -> False
// ==============================================================================
// CHECKS
// - Check for division in parent node
// - Numerator has to be addition/subtraction of a polynomial term to the power
// of 1 and a constant term, in that order OR a polynomial term to the
// power of 1
// - Denominator has to be addition/subtraction of a polynomial term to the power
// of 1 and a constant term, in that order.
// - Check to see that the denominator and numerator have the same symbol
function canFindDenominatorInNumerator(node) {
if (node.op !== '/' ) {
return false;
}
let numerator = node.args[0];
let denominator = node.args[1];
if (Node.Type.isParenthesis(numerator)) {
numerator = numerator.content;
}
if (Node.Type.isParenthesis(denominator)) {
denominator = denominator.content;
}
let numeratorArgsLength;
// If numerator has args, but it's just a polynomial term, length is 1
// Ex. 3x/2x+3 => numeratorArgsLength=1
if (Node.PolynomialTerm.isPolynomialTerm(numerator)) {
numeratorArgsLength = 1;
}
// If numerator has args and args are two seperate values length is 2
// Ex. 3x+4/2x+3 => numeratorArgsLength=2
else if (numerator.op === '+' || numerator.op === '-') {
numeratorArgsLength = numerator.args.length;
}
// If numerator doesn't have args and isn't a polynomial term, there's
// nothing the added functionality can do
// Ex. 3/(2x + 3) => False
else {
return false;
}
let denominatorArgsLength;
if (denominator.op === '+' || denominator.op === '-') {
denominatorArgsLength = denominator.args.length;
}
// If denominator doesn't have args, it's length is 1. This case is already
// resolved by splitting the denominator into all the numerators
// Ex. (x + 3)/2x => x/2x + 3/2x
else {
return false;
}
// Function doesn't support denominators with args > 2
if (denominatorArgsLength !== 2) {
return false;
}
// Check if numerator's second argument is a constant if numerator has two arguments
if (numeratorArgsLength === 2) {
if (!Node.Type.isConstant(numerator.args[1])) {
return false;
}
}
// Check if denominator's second argument is a constant
if (!Node.Type.isConstant(denominator.args[1])) {
return false;
}
// Defines the first term depending on whether there's a coefficient value
// with the first term
let numeratorFirstTerm;
if (numerator.op === '+') {
numeratorFirstTerm = new Node.PolynomialTerm(numerator.args[0]);
}
else {
numeratorFirstTerm = new Node.PolynomialTerm(numerator);
}
let denominatorFirstTerm;
if (denominator.op === '+') {
denominatorFirstTerm = new Node.PolynomialTerm(denominator.args[0]);
}
// If an exponent exists (aka not x^1), return false
if (numeratorFirstTerm.getExponentNode() ||
denominatorFirstTerm.getExponentNode()) {
return false;
}
// Check that the symbols are the same, Ex. (x+1)/(y+1) would not pass
if (!(numeratorFirstTerm.getSymbolName() ===
denominatorFirstTerm.getSymbolName())) {
return false;
}
return true;
}
module.exports = canFindDenominatorInNumerator;