From 4c02c9f13716480e3be2fb8845dbc064c347f0b2 Mon Sep 17 00:00:00 2001 From: Noratrieb <48135649+Noratrieb@users.noreply.github.com> Date: Sat, 10 Jan 2026 20:45:40 +0100 Subject: [PATCH] Fix duplicate practice answers Fixe #3 --- flashcard.js | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/flashcard.js b/flashcard.js index 8761135..e6888ee 100644 --- a/flashcard.js +++ b/flashcard.js @@ -1,19 +1,18 @@ -const randomItem = (not) => { - let item; - - do { - const idx = Math.floor(Math.random() * window.ITEMS.length); - item = window.ITEMS[idx]; - } while (not && item === not); - - return item; -}; - const infoText = document.querySelector("#info-text"); function next() { infoText.textContent = ""; - const item = randomItem(); + + const candiates = [...window.ITEMS]; + + const nextRandomItem = () => { + const idx = Math.floor(Math.random() * candiates.length); + const result = candiates[idx]; + candiates.splice(idx, 1); + return result; + }; + + const item = nextRandomItem(); const mangle = Math.random() > 0.5; @@ -24,10 +23,10 @@ function next() { children[1].textContent = mangle ? item.means : item.mangle; const options = [ - randomItem(item), - randomItem(item), - randomItem(item), - randomItem(item), + nextRandomItem(item), + nextRandomItem(item), + nextRandomItem(item), + nextRandomItem(item), ]; const correctIdx = Math.floor(Math.random() * options.length); options[correctIdx] = item;