Skip to content

Commit 4c02c9f

Browse files
committed
Fix duplicate practice answers
Fixe #3
1 parent 416f658 commit 4c02c9f

1 file changed

Lines changed: 15 additions & 16 deletions

File tree

flashcard.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
const randomItem = (not) => {
2-
let item;
3-
4-
do {
5-
const idx = Math.floor(Math.random() * window.ITEMS.length);
6-
item = window.ITEMS[idx];
7-
} while (not && item === not);
8-
9-
return item;
10-
};
11-
121
const infoText = document.querySelector("#info-text");
132

143
function next() {
154
infoText.textContent = "";
16-
const item = randomItem();
5+
6+
const candiates = [...window.ITEMS];
7+
8+
const nextRandomItem = () => {
9+
const idx = Math.floor(Math.random() * candiates.length);
10+
const result = candiates[idx];
11+
candiates.splice(idx, 1);
12+
return result;
13+
};
14+
15+
const item = nextRandomItem();
1716

1817
const mangle = Math.random() > 0.5;
1918

@@ -24,10 +23,10 @@ function next() {
2423
children[1].textContent = mangle ? item.means : item.mangle;
2524

2625
const options = [
27-
randomItem(item),
28-
randomItem(item),
29-
randomItem(item),
30-
randomItem(item),
26+
nextRandomItem(item),
27+
nextRandomItem(item),
28+
nextRandomItem(item),
29+
nextRandomItem(item),
3130
];
3231
const correctIdx = Math.floor(Math.random() * options.length);
3332
options[correctIdx] = item;

0 commit comments

Comments
 (0)