From 3761a3a07a18c4c1801ee8d3237905b9c2f503c6 Mon Sep 17 00:00:00 2001 From: ash-heinz Date: Tue, 19 May 2026 23:01:28 +0530 Subject: [PATCH] Fix: fixed math quiz giving invalid options in some questions I made it so that the options for the prime number question is fixed rather than generating random numbers --- web-app/js/projects/math-quiz.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/web-app/js/projects/math-quiz.js b/web-app/js/projects/math-quiz.js index a3a3f84..b08a6df 100644 --- a/web-app/js/projects/math-quiz.js +++ b/web-app/js/projects/math-quiz.js @@ -221,6 +221,13 @@ function initMathQuiz() { let question, correct; + // fixedOptions: when a question type has a known, constrained set of valid + // answers (e.g. prime uses only 1 or 2)we attach the exact options here + // so showQuestion() skips generateOptions() which would produce arbitrary + // nearby numbers that are meaningless for the question being asked. + let fixedOptions = null; + + if (type === 'add') { const [a, b] = [rand(1, 60), rand(1, 60)]; question = `${a} + ${b} = ?`; @@ -276,6 +283,11 @@ function initMathQuiz() { question = `Is ${num} prime? (1 = Yes, 2 = No)`; correct = isPrime(num) ? 1 : 2; + // the only valid answers are 1 (yes) or 2 (no). using generateOptions() + // here would fill the remaining 2 slots with random nearby integers + // (e.g. 13, 11, 3) that are irrelevant and confusing. + fixedOptions = [1, 2]; + } else if (type === 'conversion') { const kind = rand(0, 2); if (kind === 0) { @@ -416,8 +428,12 @@ function initMathQuiz() { if (streak >= 6 && difficulty < 3) difficulty = 3; else if (streak >= 3 && difficulty < 2) difficulty = 2; - const { question, correct } = generateQuestion(difficulty); - const options = generateOptions(correct); + const { question, correct, fixedOptions } = generateQuestion(difficulty); + + // use fixedOptions when the question has a constrained answer domain + // (e.g. prime Yes/No); otherwise generate plausible numeric distractors. + const options = fixedOptions ? fixedOptions : generateOptions(correct); + const correctIdx = options.indexOf(correct); board.innerHTML = `

❓ ${question}

`;