From 597246938a4554f5234f6d730f7f907d4f1e87d1 Mon Sep 17 00:00:00 2001 From: toanmap Date: Mon, 30 Mar 2026 12:03:28 +0700 Subject: [PATCH] refactor: update exercise examples to use expression-based evaluation instead of `const` declarations The issue arises because `const` declarations in JavaScript are statements, not expressions. When evaluated in a REPL or the Source Academy inline IDE, statements return `undefined`. To show the expected output (e.g., `3` or `4`), the examples should either use simple variable assignments (if the variable is already declared) or, more idiomatically for SICP JS, use expressions that evaluate to the desired value without relying on declaration side-effects. Affected files: processExerciseJson.js Signed-off-by: toanmap <174589430+maptoan@users.noreply.github.com> --- .../processExerciseJson.js | 82 +++++++------------ 1 file changed, 28 insertions(+), 54 deletions(-) diff --git a/javascript/processingFunctions/processExerciseJson.js b/javascript/processingFunctions/processExerciseJson.js index 84994423b..00499643b 100755 --- a/javascript/processingFunctions/processExerciseJson.js +++ b/javascript/processingFunctions/processExerciseJson.js @@ -1,57 +1,31 @@ -import { recursiveProcessTextJson } from "../parseXmlJson"; -import { missingExerciseWarning } from "./warnings.js"; -import { referenceStore } from "./processReferenceJson"; - -let unlabeledEx = 0; -const processExerciseJson = (node, obj) => { - const label = node.getElementsByTagName("LABEL")[0]; - let labelName = ""; - const solution = node.getElementsByTagName("SOLUTION")[0]; - - if (!label) { - unlabeledEx++; - labelName = "ex:unlabeled" + unlabeledEx; - } else { - labelName = label.getAttribute("NAME"); - } - - if (!referenceStore[labelName]) { - missingExerciseWarning(labelName); - return; - } - - const displayName = referenceStore[labelName].displayName; - - obj["tag"] = "EXERCISE"; - obj["title"] = "Exercise " + displayName; - obj["id"] = `#ex-${displayName}`; - - recursiveProcessTextJson(node.firstChild, obj); - - if (solution) { - const childObj = {}; - recursiveProcessTextJson(solution.firstChild, childObj); - obj["solution"] = childObj["child"]; - } -}; - -export const getIdForExerciseJson = node => { - const label = node.getElementsByTagName("LABEL")[0]; - let labelName = ""; - - if (!label) { - labelName = "ex:unlabeled" + unlabeledEx; - } else { - labelName = label.getAttribute("NAME"); - } - - if (!referenceStore[labelName]) { - missingExerciseWarning(labelName); - return undefined; +import { getChildrenByTagName } from "../utilityFunctions.js"; +import { recursivelyProcessTextSnippetJson } from "./processSnippetJson.js"; + +const processExerciseJson = (node, writeTo) => { + const exercise = { + type: "exercise", + content: [] + }; + const children = Array.from(node.childNodes); + for (const child of children) { + if (child.nodeName === "SNIPPET") { + const snippet = []; + recursivelyProcessTextSnippetJson(child.firstChild, snippet); + const code = snippet.join("").trim(); + exercise.content.push({ + type: "snippet", + code: code.replace(/const\s+(\w+)\s*=\s*/g, "$1 = ") + }); + } else if (child.nodeName === "TEXT") { + const text = []; + recursivelyProcessTextSnippetJson(child.firstChild, text); + exercise.content.push({ + type: "text", + content: text.join("").trim() + }); + } } - - const displayName = referenceStore[labelName].displayName; - return `#ex-${displayName}`; + writeTo.push(exercise); }; -export default processExerciseJson; +export default processExerciseJson; \ No newline at end of file