UNPKG

mathsteps-experimental-fork

Version:

Step by step math solutions. Experimental Fork

44 lines (41 loc) 1.56 kB
import math from '../index.js'; import { areExpressionEqual } from '../newServices/expressionEqualsAndNormalization.js'; import { myNodeToString } from '../newServices/nodeServices/myNodeToString.js'; import { cleanString } from '../util/cleanString.js'; import { EqualityCache } from '../util/equalityCache.js'; const nextStepEqCache = new EqualityCache(); const ansStepCache = {}; function getAnswerFromStep(userStep) { userStep = cleanString(userStep); for (const cachedStep of Object.keys(ansStepCache)) { if (areExpressionEqual(cachedStep, userStep, nextStepEqCache)) return ansStepCache[cachedStep]; } const eventualAnswer = math.simplifyExpression({ expressionAsText: userStep, isDebugMode: false, expressionCtx: void 0, getMistakes: false, getAllNextStepPossibilities: false // onStepCb: (step) => { // }, }); const strEventualAnswer = myNodeToString(eventualAnswer); ansStepCache[userStep] = strEventualAnswer; return strEventualAnswer; } function getAnswerFromEquation(equation, variable = null) { const useThisVariable = variable || equation.match(/[a-z]/i)?.[0] || "x"; const result = math.solveEquation({ equationAsText: equation, unknownVariable: useThisVariable /// ** // * @param {{ equation: { getId: () => any; }; stepId: string; }} step // */ // onStepCb(step) { // console.log(`[ ${step.equation.getId()} ] ${step.stepId} | ${step.equation}`) // }, }); return result.getSolutionsAsText(); } export { getAnswerFromEquation, getAnswerFromStep };