lib/simplifyExpression/fractionsSearch/addConstantFractions.js
// Returns a Node.Status object with substeps
Returns a Node.Status object with substeps
Returns a Node . Status object with substeps
function addConstantFractions(node) {
  let newNode = node.cloneDeep();
  if (!Node.Type.isOperator(node) || node.op !== '+') {
    return Node.Status.noChange(node);
  }
  if (!node.args.every(n => Node.Type.isIntegerFraction(n, true))) {
    return Node.Status.noChange(node);
  }
  const denominators = node.args.map(fraction => {
    return parseFloat(evaluate(fraction.args[1]));
  });
  const substeps = [];
  let status;
  if (!denominators.every(denominator => denominator === denominators[0])) {
    status = makeCommonDenominator(newNode);
    substeps.push(status);
    newNode = Node.Status.resetChangeGroups(status.newNode);
    status = evaluateDenominators(newNode);
    substeps.push(status);
    newNode = Node.Status.resetChangeGroups(status.newNode);
    status = evaluateNumerators(newNode);
    substeps.push(status);
    newNode = Node.Status.resetChangeGroups(status.newNode);
  }
  status = combineNumeratorsAboveCommonDenominator(newNode);
  substeps.push(status);
  newNode = Node.Status.resetChangeGroups(status.newNode);
  status = addNumeratorsTogether(newNode);
  substeps.push(status);
  newNode = Node.Status.resetChangeGroups(status.newNode);
  status = reduceNumerator(newNode);
  if (status.hasChanged()) {
    substeps.push(status);
    newNode = Node.Status.resetChangeGroups(status.newNode);
  }
  status = divideByGCD(newNode);
  if (status.hasChanged()) {
    substeps.push(status);
    newNode = Node.Status.resetChangeGroups(status.newNode);
  }
  return Node.Status.nodeChanged(
    ChangeTypes.ADD_FRACTIONS, node, newNode, true, substeps);
}
function addConstantFractions ( node ) { let newNode = node . cloneDeep ( ) ; if ( ! Node . Type . isOperator ( node ) || node . op !== '+' ) { return Node . Status . noChange ( node ) ; } if ( ! node . args . every ( n => Node . Type . isIntegerFraction ( n , true ) ) ) { return Node . Status . noChange ( node ) ; } const denominators = node . args . map ( fraction => { return parseFloat ( evaluate ( fraction . args [ 1 ] ) ) ; } ) ; const substeps = [ ] ; let status ; if ( ! denominators . every ( denominator => denominator === denominators [ 0 ] ) ) { status = makeCommonDenominator ( newNode ) ; substeps . push ( status ) ; newNode = Node . Status . resetChangeGroups ( status . newNode ) ; status = evaluateDenominators ( newNode ) ; substeps . push ( status ) ; newNode = Node . Status . resetChangeGroups ( status . newNode ) ; status = evaluateNumerators ( newNode ) ; substeps . push ( status ) ; newNode = Node . Status . resetChangeGroups ( status . newNode ) ; } status = combineNumeratorsAboveCommonDenominator ( newNode ) ; substeps . push ( status ) ; newNode = Node . Status . resetChangeGroups ( status . newNode ) ; status = addNumeratorsTogether ( newNode ) ; substeps . push ( status ) ; newNode = Node . Status . resetChangeGroups ( status . newNode ) ; status = reduceNumerator ( newNode ) ; if ( status . hasChanged ( ) ) { substeps . push ( status ) ; newNode = Node . Status . resetChangeGroups ( status . newNode ) ; } status = divideByGCD ( newNode ) ; if ( status . hasChanged ( ) ) { substeps . push ( status ) ; newNode = Node . Status . resetChangeGroups ( status . newNode ) ; } return Node . Status . nodeChanged ( ChangeTypes . ADD_FRACTIONS , node , newNode , true , substeps ) ; }
Returns a Node.Status object with substeps
Returns a Node . Status object with substeps
// 1A. First create the common denominator if needed // e.g. 2/6 + 1/4 -> (2*2)/(6*2) + (1*3)/(4*3) // 1B. Multiply out the denominators // 1B. Multiply out the numerators // 2A. Now that they all have the same denominator, combine the numerators // e.g. 2/3 + 5/3 -> (2+5)/3 // 2B. Finally, add the numerators together // 2C. If the numerator is 0, simplify to just 0 // 2D. If we can simplify the fraction, do so