mathsteps-experimental-fork
Version:
Step by step math solutions. Experimental Fork
71 lines (67 loc) • 2.51 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
function findMultiplyBeforeAddMistakes(expressionString) {
if (!expressionString.includes("*") || !expressionString.includes("+")) {
return [];
}
const rgNum = "\\d+(?:\\.\\d+)?";
const rgAdd = "\\+";
const rgMult = "\\*";
const interpretation1Regex = new RegExp(`(${rgNum})${rgAdd}(${rgNum})${rgMult}`, "g");
const interpretation2Regex = new RegExp(`${rgMult}(${rgNum})${rgAdd}(${rgNum})`, "g");
const alternativeInterpretations = /* @__PURE__ */ new Set();
function generateAlternative(expression, match, startIndex, endIndex, calculatedPart) {
return expression.slice(0, startIndex) + calculatedPart + expression.slice(endIndex);
}
processMatches(interpretation1Regex, expressionString, (match) => {
const [fullMatch, num1, num2] = match;
const addedValue = Number.parseFloat(num1) + Number.parseFloat(num2);
const newExpression = generateAlternative(
expressionString,
fullMatch,
match.index,
match.index + fullMatch.length,
`${addedValue}*${match[0].split("*")[1]}`
);
alternativeInterpretations.add(newExpression);
});
processMatches(interpretation2Regex, expressionString, (match) => {
const [fullMatch, num1, num2] = match;
const addedValue = Number.parseFloat(num1) + Number.parseFloat(num2);
const newExpression = generateAlternative(
expressionString,
fullMatch,
match.index,
match.index + fullMatch.length,
`${match[0].split("*")[0]}*${addedValue}`
);
alternativeInterpretations.add(newExpression);
});
return Array.from(alternativeInterpretations);
}
function mistakeSearches(start) {
const addBeforeMultMistakes = [];
findMultiplyBeforeAddMistakes(start).forEach((mistake) => {
addBeforeMultMistakes.push({
from: start,
to: mistake,
changeType: "PEMDAS__ADD_INSTEAD_OF_MULTIPLY",
isMistake: true,
attemptedToGetTo: "UNKNOWN",
availableChangeTypes: []
});
});
const pemdasAddBeforeMultMistake = addBeforeMultMistakes[0] ? {
...addBeforeMultMistakes[0],
mTo: addBeforeMultMistakes
} : null;
return [pemdasAddBeforeMultMistake].filter((mistake) => mistake);
}
function processMatches(regex, str, handler) {
let match;
while ((match = regex.exec(str)) !== null) {
handler(match);
}
}
exports.findMultiplyBeforeAddMistakes = findMultiplyBeforeAddMistakes;
exports.mistakeSearches = mistakeSearches;