Skip to content

Commit

Permalink
Add code to ensure no consecutive questions are selected
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzsche committed Jun 26, 2024
1 parent 5e5ee32 commit 8e35f26
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ function pick(questions, num) {
// complete question catalog by buidling packages of question
// we draw questions one at a time

// the final flag is used if the random number generator choose the last
// element of a package. In this case the next pack with select from 2nd in package
// to avoid the to consecutive numbers are selected.

final = false;
for(let rem_num=num;rem_num>0;rem_num--) {
let pack_size = Math.floor( questions.length / rem_num )
let pack_mod = questions.length % rem_num
if (pack_mod > 0) pack_size++
let r = getRandomInt(0, pack_size)
let r = getRandomInt(final?1:0, pack_size)
final = (pack_size == r + 1) ? true : false;
result.push(questions[r])
questions.splice(0, pack_size);
}
Expand Down

0 comments on commit 8e35f26

Please sign in to comment.