Mercurial Hosting > arkian
view src/test.js @ 7:a94c1043ce05
mikraite
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sat, 13 Sep 2025 18:09:47 -0600 |
parents | 45a3989c3447 |
children |
line wrap: on
line source
// 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> ` ; }