In order to create a quiz, I am looking to extract 'questions' from various 'topic' arrays. These topics are selected based on the user's preference and are used to populate a question bank for a 20-question quiz.
The topics rated as most important will contribute more questions to the pool of 20 questions. However, devising an algorithm to achieve this has proven to be quite challenging for me.
I have attempted to iterate over the parent topics array (which contains all topic objects with properties like name:str and question:[]). By determining the average number of questions needed to compile 20 questions from all the topics, I calculated any excess questions that may result.
For instance, if there are 6 topics, each should provide 3.3 questions on average. Rounded up to 4 questions per topic, we end up with 24 total questions - an overhead of 4.
My struggle lies in subtracting this overhead from the number of questions to be extracted from the least important rated topics, with the last item in the array being the least crucial.
function createWeightedQuestionBank(topicsArray) {
// Object to store the returned questions.
let questionsBanks = [];
// Total number of questions desired in our quiz/question bank.
const questionsLimit = 20;
// Topics passed in through the topics Array - each Topic is an object with {name: "topicName", questions: [q1,q2,q3,q4]}
const topics = topicsArray;
if (topics) {
// Number of topics in the topics array to be included in the question bank.
// TODO: deciding which topics to include or exclude.
const topicsAmount = topics.length;
// Average amount of questions to be taken from each group to reach 20 questions (rounded up).
const questionsAverage = Math.ceil(questionsLimit / topicsAmount);
// Calculating projected number of questions when averaging from each group.
const projectedQuestions = (questionsAverage * topicsAmount);
let overhead;
if (questionsLimit > projectedQuestions ) {
overhead = questionsLimit - projectedQuestions;
} else {
overhead = projectedQuestions - questionsLimit;
}
let overheadVariance = overhead;
for ( let i = 0; i < topics.length; i++) {
const topic = topics[i];
let pullAmount;
if (topics.length - (overhead - 1) <= i) {
pullAmount = questionsAverage - (overheadVariance - overhead);
overheadVariance++;
} else {
pullAmount = questionsAverage;
}
console.log(topics.length - overhead);
Representation of the topics array.
this.topics = [
{
name: 'testy',
isSelected: false,
questions:
[
'one',
'two',
'three',
'one',
'two',
'three',
'four',
]
},
{
name: 'testy1',
isSelected: false,
questions:
[
'one',
'one',
'one',
'one',
'two',
'three',
'four',
]
},
{
name: 'test2',
isSelected: false,
questions:
[
'one',
'two',
'three',
'four',
]
},
...
];
I am currently struggling to deduce how to substract the overhead of 4 from the last 3 topic question arrays. Instead of extracting 4 questions (the average amount), I aim to pull 3, 2, 1 questions from the three least important topics.
The desired outcome would be to pull 4 questions from some topics, followed by pulling 3, 2, 1 questions respectively from the less significant topics, totaling 20 questions. However, my current implementation is only logging 2 six times instead.