UNPKG

mathsteps-experimental-fork

Version:

Step by step math solutions. Experimental Fork

99 lines (94 loc) 4.9 kB
'use strict'; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } }); const utils = require('./utils.js'); const SimpleSolveEquationFunction = require('../kemuEquation/SimpleSolveEquationFunction.js'); const expressionEqualsAndNormalization = require('../newServices/expressionEqualsAndNormalization.js'); const changeAndMistakeUtils = require('../types/changeType/changeAndMistakeUtils.js'); const ChangeTypes = require('../types/changeType/ChangeTypes.js'); const MathRuleTypes = require('../types/changeType/MathRuleTypes.js'); const UndoableString = require('../util/UndoableString.js'); function getEquationMatches(currentEquation) { const startingLeftRight = utils.makeStartingLeftAndRightEqSteps(currentEquation); const eqProcessedSteps = utils.makeEquationProcessSteps(currentEquation); const addRemoveCombo = utils.findMatchingAddRemoveTerms(eqProcessedSteps); const nonAddRemoveRuleMatches = utils.findNonAddRemoveRuleMatches( eqProcessedSteps, startingLeftRight ); const allRuleMatches = addRemoveCombo.concat(nonAddRemoveRuleMatches).map((option) => ({ left: option.left, right: option.right, newTo: `${option.left.to}=${option.right.to}` // full equation after applying that step })); return allRuleMatches; } const getFastestAmountOfStepsToSolveEquation = (assessedSteps) => { const removeNoChangeTypeStepsFn = (side) => side.filter((s) => s.attemptedChangeType !== ChangeTypes.ChangeTypes.NO_CHANGE); const removeStuffFromRightFn = (side) => { return side.filter((s) => !changeAndMistakeUtils.isRemoveTermChangeType(s.attemptedChangeType)).filter((s) => !changeAndMistakeUtils.isAddTermChangeType(s.attemptedChangeType)).filter((s) => s.attemptedChangeType !== ChangeTypes.ChangeTypes.EQ_SWAP_SIDES).filter((s) => s.attemptedChangeType !== ChangeTypes.ChangeTypes.EQ_MULTIPLY_BOTH_SIDES_BY_NEGATIVE_ONE); }; const removeRemoveAddingZeroFn = (side) => side.filter((s) => s.attemptedChangeType !== ChangeTypes.ChangeTypes.REMOVE_ADDING_ZERO); assessedSteps = assessedSteps.map((step) => { const left = removeRemoveAddingZeroFn(removeNoChangeTypeStepsFn(step.left)); const right = removeRemoveAddingZeroFn(removeNoChangeTypeStepsFn(removeStuffFromRightFn(step.right))); return { ...step, left, right }; }); let count = 0; for (const step of assessedSteps) { count += step.left.length; count += step.right.length; } return count; }; class EquationCommander { equationHistory; finalCorrectAnswer; correctAnswerSteps; assessedSteps; constructor(initialValue = "") { this.equationHistory = new UndoableString.UndoableString(initialValue); const { steps, assessedSteps } = SimpleSolveEquationFunction.getAssessSolvedProblemSteps(initialValue); this.correctAnswerSteps = steps; this.assessedSteps = assessedSteps; this.finalCorrectAnswer = steps[steps.length - 1].equationString; } redo = () => this.equationHistory.redo(); undo = () => this.equationHistory.undo(); setValue = (newValue) => this.equationHistory.setValue(newValue); getValue = () => this.equationHistory.getValue(); isSolved = () => expressionEqualsAndNormalization.areEquationsEqual(this.finalCorrectAnswer, this.getValue()); // TODO add cache getHistory = () => this.equationHistory.getHistory(); getFuture = () => this.equationHistory.getFuture(); swap() { const currentEquation = this.getValue(); const [left, right] = currentEquation.split("="); this.setValue(`${right}=${left}`); } getAssessedSteps = () => this.assessedSteps; getCorrectAnswerSteps = () => this.correctAnswerSteps; getFastestAmountOfStepsToSolve = () => getFastestAmountOfStepsToSolveEquation(this.assessedSteps); getCurrentNumberOfSteps = () => this.assessedSteps.length; getCurrentMatches() { const allRuleMatches = getEquationMatches(this.getValue()); const skippedZeroPlusMatches = allRuleMatches.map((match) => { const cleanStartingZeroToFn = (to) => to.replace(/^0[a-z]*[\+\-]/gmi, ""); const left = cleanStartingZeroToFn(match.left.to); const right = cleanStartingZeroToFn(match.right.to); match.left.to = left; match.right.to = right; match.newTo = `${left}=${right}`; return match; }); return skippedZeroPlusMatches; } getMatchesForRule(rule) { const matches = this.getCurrentMatches(); const changeTypesForGivenRule = rule !== "all" ? MathRuleTypes.getChangesTypesForRule(rule) : Object.values(ChangeTypes.ChangeTypes).flat(); return matches.filter((match) => changeTypesForGivenRule.includes(match.left.changeType) || changeTypesForGivenRule.includes(match.right.changeType)); } } exports.EquationCommander = EquationCommander; exports.default = EquationCommander; exports.getFastestAmountOfStepsToSolveEquation = getFastestAmountOfStepsToSolveEquation;