0
|
1 // must load ot.js first
|
|
2
|
|
3 'use strict';
|
|
4
|
|
5 let nQuestions = 10;
|
|
6
|
|
7 for( let i=0; i<allQuestions.length; i++ ) {
|
|
8 allQuestions[i].i = i;
|
|
9 }
|
|
10
|
|
11 console.log(`using ${nQuestions} out of ${allQuestions.length} questions`)
|
|
12
|
|
13
|
|
14 function remove(a,i) {
|
|
15 return a.splice(i,1)[0];
|
|
16 }
|
|
17
|
|
18 function removeRandom(a) {
|
|
19 let i = Math.floor( Math.random() * a.length );
|
|
20 return remove(a,i);
|
|
21 }
|
|
22
|
|
23 let questions = [];
|
|
24 let allQuestions2 = JSON.parse(JSON.stringify(allQuestions));
|
|
25 for( let i=0; i<nQuestions; i++ ) {
|
|
26 let p = removeRandom(allQuestions2);
|
|
27 let a = [];
|
|
28 while( p.a.length > 0 ) {
|
|
29 a.push( removeRandom(p.a) );
|
|
30 }
|
|
31 p.a = a;
|
|
32 questions.push(p);
|
|
33 }
|
|
34
|
|
35 let encoder = document.createElement('span');
|
|
36
|
|
37 function htmlEncode(s) {
|
|
38 encoder.textContent = s;
|
|
39 return encoder.innerHTML;
|
|
40 }
|
|
41
|
|
42 function checkTest() {
|
|
43 let divs = document.querySelectorAll('div[q]');
|
|
44 let nCorrect = 0;
|
|
45 for( let div of divs ) {
|
|
46 let answer = allQuestions[div.getAttribute('q')].a[0];
|
|
47 let selected = div.querySelector('input[type=radio]:checked');
|
|
48 if( selected.value === answer ) {
|
|
49 selected.parentNode.insertAdjacentHTML( 'afterend', ' <span correct></span>' );
|
|
50 nCorrect++;
|
|
51 } else {
|
|
52 selected.parentNode.insertAdjacentHTML( 'afterend', ' <span wrong></span>' );
|
|
53 let correct = div.querySelector(`input[type=radio][value="${htmlEncode(answer)}"]`);
|
|
54 correct.parentNode.insertAdjacentHTML( 'afterend', ' <span correct></span>' );
|
|
55 }
|
|
56 }
|
|
57 document.querySelector('div[end]').innerHTML = `\
|
|
58 <p>${nCorrect} of ${divs.length} correct</p>
|
|
59 <p><a href="/test.html">Try again</a></p>
|
|
60 ` ;
|
|
61 }
|