mathsteps
Version:
Step by step math solutions
38 lines (29 loc) • 988 B
JavaScript
const checks = require('../checks');
const factorQuadratic = require('./factorQuadratic');
const flattenOperands = require('../util/flattenOperands');
const removeUnnecessaryParens = require('../util/removeUnnecessaryParens');
// Given a mathjs expression node, steps through factoring the expression.
// Currently only supports factoring quadratics.
// Returns a list of details about each step.
function stepThrough(node, debug=false) {
if (debug) {
// eslint-disable-next-line
console.log('\n\nFactoring: ' + print.ascii(node, false, true));
}
if (checks.hasUnsupportedNodes(node)) {
return [];
}
let nodeStatus;
const steps = [];
node = flattenOperands(node);
node = removeUnnecessaryParens(node, true);
if (checks.isQuadratic(node)) {
nodeStatus = factorQuadratic(node);
if (nodeStatus.hasChanged()) {
steps.push(nodeStatus);
}
}
// Add factoring higher order polynomials...
return steps;
}
module.exports = stepThrough;