From ffd03befc98a50b651fb5c117ca96201142536a7 Mon Sep 17 00:00:00 2001 From: JoshdRod Date: Mon, 24 Nov 2025 20:06:40 +0000 Subject: [PATCH 1/4] Added positive expression evaluator Cannot handle negative coefficients for expressions. --- src/script.js | 68 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 59 insertions(+), 9 deletions(-) diff --git a/src/script.js b/src/script.js index cdd100b..4dcbd88 100644 --- a/src/script.js +++ b/src/script.js @@ -45,20 +45,70 @@ function checkValidInputExpression() { // RETURNS: (enum?) colour (red, yellow, green) function evaluateInputExpression(answer) { - let expressionDict = expressionToDict(answer) - let colour = determineColourFromDict(expressionDict); - - return colour; + let expressionDict = expressionToDict(answer) + let colour = determineColourFromDict(expressionDict); + + return colour; } +//TODO: Make correctly handle negative terms // Cuts expression into a terms:coeffs dictionary // INPUT: str some valid maths expression // RETURNS: (dict) of terms:coeff values // e.g: x ^ 2 + 3 sin(4x) - 2 sin(x)cos(x) - x // -> {x^2 : 1, sin(4x) : 3, sin(x)cos(x) : -2, x : -1} -function expressionToDict(expresssion) { - rawExpresssion = expression.replaceAll(" ", ""); - - // Strip all terms from raw expression and add to dict - return ""; +function expressionToDict(expression) { + let rawExp = expression.replaceAll(" ", ""); + let i = 0; + let decomposedExpression = {}; + while (i < rawExp.length) + { + nextTerm = termToDict(rawExp, i); + decomposedExpression[nextTerm.term] = nextTerm.coeff; + i = nextTerm.i; + } + return decomposedExpression; +} + +// "3x^2" -> [x^2, 3] +function termToDict(str, i) { + let startTerm = -1; + let endTerm = str.length; + // j = start of term + for (let j = i; j < str.length; j++) + { + if (isNaN(str[j])) + { + startTerm = j; + break; + } + } + + let bracketCount = 0; + + // k = end of term + for (let k = startTerm; k < str.length; k++) + { + if (str[k] == '(') + { + bracketCount++; + } + else if (str[k] == ')') + { + bracketCount--; // FIXME: Won't work for brackted terms - e.g (10x + 2x^2)^1/2' + } + if (str[k] == '+' || str[k] == '-') + { + if (bracketCount == 0) + { + endTerm = k; + break; + } + } + } + return { + coeff : str.slice(i, startTerm), + term: str.slice(startTerm, endTerm), + i: endTerm + 1 + }; } From 7bd92f15a213447d71880a4440e4d16e7ef57d50 Mon Sep 17 00:00:00 2001 From: JoshdRod Date: Mon, 24 Nov 2025 20:33:44 +0000 Subject: [PATCH 2/4] Added support for converting negative and solo (1) terms to dict --- src/script.js | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/script.js b/src/script.js index 4dcbd88..fa4b270 100644 --- a/src/script.js +++ b/src/script.js @@ -74,6 +74,19 @@ function expressionToDict(expression) { function termToDict(str, i) { let startTerm = -1; let endTerm = str.length; + + // Deal with first part of expression. If +, skip it. If -, take note and move on one. + let isNegative = false; + if (str[i] == '+') + { + i++; + } + else if (str[i] == '-') + { + i++; + isNegative = true; + } + // j = start of term for (let j = i; j < str.length; j++) { @@ -106,9 +119,19 @@ function termToDict(str, i) { } } } + // Compose coeff + let coefficient = str.slice(i, startTerm); + if (coefficient == '') + { + coefficient = '1'; + } + if (isNegative) + { + coefficient = '-' + coefficient; + } return { - coeff : str.slice(i, startTerm), + coeff : coefficient, term: str.slice(startTerm, endTerm), - i: endTerm + 1 + i: endTerm }; } From c087cb7c4de1c9a471d9fb7de87a87ca0b32f71c Mon Sep 17 00:00:00 2001 From: JoshdRod Date: Mon, 24 Nov 2025 20:39:51 +0000 Subject: [PATCH 3/4]