Randomize order of options
This commit is contained in:
parent
f091b318d4
commit
82aed19f85
9
src/rand.ts
Normal file
9
src/rand.ts
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
export default function randomize<T>(input: T[]): T[] {
|
||||||
|
let res: T[] = [];
|
||||||
|
input = [...input];
|
||||||
|
while (input.length > 0) {
|
||||||
|
let randomIndex = Math.floor(Math.random() * input.length);
|
||||||
|
res.push(...input.splice(randomIndex, 1));
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
@ -1,10 +1,12 @@
|
|||||||
<script>
|
<script>
|
||||||
|
import randomize from "../../rand.ts";
|
||||||
|
|
||||||
export let question;
|
export let question;
|
||||||
export let showResult = false;
|
export let showResult = false;
|
||||||
export let isCorrect = false;
|
export let isCorrect = false;
|
||||||
|
|
||||||
|
|
||||||
$: options = Object.keys(question.options).map(e => ({ key: e, value: question.options[e] }));
|
$: options = randomize(Object.keys(question.options).map(e => ({ key: e, value: question.options[e] })));
|
||||||
|
|
||||||
let selected = [];
|
let selected = [];
|
||||||
$: isCorrect = selected.length === question.correct.length && selected.every(val => question.correct.find(v => v === val));
|
$: isCorrect = selected.length === question.correct.length && selected.every(val => question.correct.find(v => v === val));
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
<script>
|
<script>
|
||||||
|
import randomize from "../../rand.ts";
|
||||||
|
|
||||||
export let question;
|
export let question;
|
||||||
export let showResult = false;
|
export let showResult = false;
|
||||||
|
|
||||||
export let isCorrect = false;
|
export let isCorrect = false;
|
||||||
|
|
||||||
|
$: options = randomize(Object.keys(question.options).map(e => ({ key: e, value: question.options[e] })));
|
||||||
$: options = Object.keys(question.options).map(e => ({ key: e, value: question.options[e] }));
|
|
||||||
|
|
||||||
let selected = undefined;
|
let selected = undefined;
|
||||||
$: isCorrect = selected === question.correct;
|
$: isCorrect = selected === question.correct;
|
||||||
|
Loading…
Reference in New Issue
Block a user