Question bank for online exam

Hi all,

I want to make an online exam with 20 questions and want the 20 questions to be randomly selected from a database so every test would be unique. Is there any way to do this? Maybe through some advanced forms? thanks!

It depends on how many questions you have in your database collection.
If you don’t have too many, the easiest way will be to retrieve them all and then select 20 random questions, if you have a lot of questions you can go for another approach.
For sake of the example, I’ll assume you don’t have more than 50 questions.
So you can do something like:

import wixData from 'wix-data';
Promise.all([
 wixData.query('CollectionName').find(),
 new Promise((resolve, reject) => $w.onReady(() => resolve()))
])
.then(r => {
 let questions = r[0].items;
 for (let i = questions.length - 1; i > 0; i--) {
  const j = Math.floor(Math.random() * (i + 1));
  [questions[i], questions[j]] = [questions[j],questions[i]];
    }
if(questions.length > 20){questions.length = 20;}
$w('#repeater').data = questions;
$w('#repeater').forEachItem(($i, iData) => {
 $i('#title').text = iData.title;//for example
 $i('#image').src = iData.image;//for example
 //etc...
 })
})

Thanks for answer!
I have 125 question to be exact. Will this solution work for me or should I Go for another approach?

Thanks for the help!

It should work,
but add:

wixData.query('CollectionName').limit(125).find()

(The default limit is 50)

if you see it’s too slow, there may be other ways to refine the query itself (especially if you don’t need each question to be random, I mean if it’s enough to have a random bunch of questions - for example 1-20 21-40, 41-80 etc…)