Avoid immediate repeat of last fact when picking next question

This commit is contained in:
chris 2025-11-20 08:57:41 -05:00
parent 53d140caf8
commit bf1e1238fc

16
app.js
View File

@ -71,7 +71,8 @@
activeSkin: 'skin-default', activeTrail: '', activeBackground: 'bg-default', activeSkin: 'skin-default', activeTrail: '', activeBackground: 'bg-default',
activePet: '', activePetIcon: '', activePet: '', activePetIcon: '',
studentName: '', studentName: '',
focusQueue: [] focusQueue: [],
lastFactId: ''
}; };
let game = { let game = {
@ -185,11 +186,19 @@
const relevantTrouble = state.troubleList.filter(t => allowedNumbers.includes(t.n1) || allowedNumbers.includes(t.n2)); const relevantTrouble = state.troubleList.filter(t => allowedNumbers.includes(t.n1) || allowedNumbers.includes(t.n2));
let n1, n2; let n1, n2;
const makeId = (a, b) => {
const [s, l] = a < b ? [a, b] : [b, a];
return `${s}x${l}`;
};
const lastId = state.lastFactId;
if (state.focusQueue.length > 0) { if (state.focusQueue.length > 0) {
const factId = state.focusQueue.shift(); const factId = state.focusQueue.shift();
const parts = factId.split('x').map(Number); const parts = factId.split('x').map(Number);
[n1, n2] = parts; [n1, n2] = parts;
} else { } else {
let attempts = 0;
while (attempts < 6) {
if (relevantTrouble.length > 0 && Math.random() < 0.5) { if (relevantTrouble.length > 0 && Math.random() < 0.5) {
const struggle = relevantTrouble[Math.floor(Math.random() * relevantTrouble.length)]; const struggle = relevantTrouble[Math.floor(Math.random() * relevantTrouble.length)];
n1 = struggle.n1; n2 = struggle.n2; n1 = struggle.n1; n2 = struggle.n2;
@ -198,9 +207,14 @@
n2 = Math.floor(Math.random() * 12) + 1; n2 = Math.floor(Math.random() * 12) + 1;
if (Math.random() > 0.5) [n1, n2] = [n2, n1]; if (Math.random() > 0.5) [n1, n2] = [n2, n1];
} }
const cid = makeId(n1, n2);
if (cid !== lastId || attempts === 5) break;
attempts++;
}
} }
game.currentFact = { n1, n2, ans: n1 * n2 }; game.currentFact = { n1, n2, ans: n1 * n2 };
state.lastFactId = makeId(n1, n2);
document.getElementById('equation-display').innerText = `${n1} × ${n2}`; document.getElementById('equation-display').innerText = `${n1} × ${n2}`;
startDementor(); startDementor();
} }