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>
|
||||
import randomize from "../../rand.ts";
|
||||
|
||||
export let question;
|
||||
export let showResult = 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 = [];
|
||||
$: isCorrect = selected.length === question.correct.length && selected.every(val => question.correct.find(v => v === val));
|
||||
|
@ -1,11 +1,12 @@
|
||||
<script>
|
||||
import randomize from "../../rand.ts";
|
||||
|
||||
export let question;
|
||||
export let showResult = 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 = undefined;
|
||||
$: isCorrect = selected === question.correct;
|
||||
|
Loading…
Reference in New Issue
Block a user