Randomize order of options

This commit is contained in:
Fabian Stamm 2019-09-11 18:36:31 +02:00
parent f091b318d4
commit 82aed19f85
3 changed files with 15 additions and 3 deletions

9
src/rand.ts Normal file
View 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;
}

View File

@ -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));

View File

@ -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;