annotate src/test.js @ 11:998dbbab9f26

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