1

I want to pull 'questions' from different 'topic' arrays to build a quiz. The topics are being selected in order of importance to the user, each topic with a varying number of questions within and are used to populate a 20 question quiz's question bank based on their selection.

The most important rated topics will have more questions within this bank of 20 questions. The algorithm to complete this giving me a hard time.

I've tried looping over the parent topics array (containing all of the topic objects with name:str and question:[] properties). Then using an average amount(rounded up in case of odd number of topics) of questions needed to create 20 questions from all the topic's questions I worked out if there is any overhead.

i.e. 6 topics = 3.3 q's from each topic, 4 rounded up which is 24 q's to be pulled = overhead of 4.

Then I am trying to takeaway the overhead from the amount of questions to be pulled from the least important rated topics. The last item in the array being the least important.

function createWeightedQuestionBank(topicsArray) {

  // Returned Object of questions.
  let questionsBanks = [];

  // How many questions we want our quiz/question bank to have.
  const questionsLimit = 20;

  // The topics are passed in via the topics Array. Each Topic is an object
  // with {name: "topicName", questions: [q1,q2,q3,q4]}
  const topics = topicsArray;

  if (topics) {

  // The amount of topics in the topics array to be included in the questions banks.
  // TODO: inclusion and exclusion of topics.
  const topicsAmount = topics.length;

  // This is the average amount of questions that should be taken from each group

  // to make 20q's (rounded up).
  const questionsAverage = Math.ceil(questionsLimit / topicsAmount);

  // If an uneven number of topics is selected rounding up is necessary, but not
  // Leaves us with too many questions when the average amount is selected from each group.
  // 20questions/6topics = 4(rounded up from each group). 6*4 = 24.
  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));

What the topics array looks like.


 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',
          ]
    },
      {
        name: 'testy3',
        isSelected: false,
        questions:
          [
            'one',
            'two',
            'three',
            'four',
          ]
    },
      {
        name: 'testy4',
        isSelected: false,
        questions:
          [
            'one',
            'two',
            'three',
            'four',
          ]
    },
      {
        name: 'testy5',
        isSelected: false,
        questions:
          [
            'one',
            'two',
            'three',
            'four',
          ]
    },
    ];

I can't figure out how to get remove the overhead of 4 from the last 3 topic question arrays. So instead of pulling 4 (the average amount to be pulled to make 20) I would pull 3 2 1 from the three least important topics.

I want the output to be 4 4 4 3 2 1 totaling 20. However it just logs 2 six times.

4

1 回答 1

0

实际上,4+4+4+3+2+118,不是20=)

如果你需要类似的东西4+4+4+4+3+1,你可以尝试:

const amountOfTopics = topics.length;
const questionsPerTopic = Math.ceil(questionsLimit / amountOfTopics);
const overhead = questionsPerTopic * topics.length - questionsLimit;
const maxQuestionsCanBeRemoved = questionsPerTopic - 1;
const singleQuestionTopics = Math.floor(overhead / maxQuestionsCanBeRemoved);
const restQuestionsToBeRemoved = overhead - singleQuestionTopics * maxQuestionsCanBeRemoved;

const getAmountOfQuestions = topicIndex => {
  if (topicIndex >= amountOfTopics - singleQuestionTopics) {
    return 1;
  }

  if (topicIndex === amountOfTopics - singleQuestionTopics - 1) {
    return questionsPerTopic - restQuestionsToBeRemoved;
  }

  return questionsPerTopic;
};

topics.flatMap(
  (topic, index) => {
    const amount = getAmountOfQuestions(index);

    return topic.questions.slice(0, amount);
  }
);
于 2019-08-13T15:59:55.907 回答