Mercurial Hosting > arkian
diff src/test.js @ 0:45a3989c3447
start public repo
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 11 Sep 2025 15:26:39 -0600 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test.js Thu Sep 11 15:26:39 2025 -0600 @@ -0,0 +1,61 @@ +// must load ot.js first + +'use strict'; + +let nQuestions = 10; + +for( let i=0; i<allQuestions.length; i++ ) { + allQuestions[i].i = i; +} + +console.log(`using ${nQuestions} out of ${allQuestions.length} questions`) + + +function remove(a,i) { + return a.splice(i,1)[0]; +} + +function removeRandom(a) { + let i = Math.floor( Math.random() * a.length ); + return remove(a,i); +} + +let questions = []; +let allQuestions2 = JSON.parse(JSON.stringify(allQuestions)); +for( let i=0; i<nQuestions; i++ ) { + let p = removeRandom(allQuestions2); + let a = []; + while( p.a.length > 0 ) { + a.push( removeRandom(p.a) ); + } + p.a = a; + questions.push(p); +} + +let encoder = document.createElement('span'); + +function htmlEncode(s) { + encoder.textContent = s; + return encoder.innerHTML; +} + +function checkTest() { + let divs = document.querySelectorAll('div[q]'); + let nCorrect = 0; + for( let div of divs ) { + let answer = allQuestions[div.getAttribute('q')].a[0]; + let selected = div.querySelector('input[type=radio]:checked'); + if( selected.value === answer ) { + selected.parentNode.insertAdjacentHTML( 'afterend', ' <span correct></span>' ); + nCorrect++; + } else { + selected.parentNode.insertAdjacentHTML( 'afterend', ' <span wrong></span>' ); + let correct = div.querySelector(`input[type=radio][value="${htmlEncode(answer)}"]`); + correct.parentNode.insertAdjacentHTML( 'afterend', ' <span correct></span>' ); + } + } + document.querySelector('div[end]').innerHTML = `\ + <p>${nCorrect} of ${divs.length} correct</p> + <p><a href="/test.html">Try again</a></p> +` ; +}