簡體   English   中英

如何根據長度不同的多個數組創建新的設定長度數組,以及根據重要性從每個較小的數組中取出的項目數

[英]How to create a new array of set length from multiple arrays of varying length, the number of items taken from each smaller arrays based on importance

我想從不同的“主題”數組中提取“問題”以建立測驗。 主題是按照對用戶的重要性進行選擇的,每個主題中包含不同數量的問題,並根據他們的選擇來填充20個問題測驗的問題庫。

在這20個問題中,評分最高的主題將有更多問題。 完成此操作的算法使我很難。

我嘗試遍歷父主題數組(包含所有具有name:str和question:[]屬性的主題對象)。 然后使用平均數量(在主題數量奇數的情況下四舍五入)從所有主題的問題中創建20個問題所需的問題,我算出是否有開銷。

即6個主題=每個主題的3.3個q,四舍五入為24個q =開銷為4。

然后,我試圖從最不重要的額定主題中抽出的問題數量減少開銷。 數組中的最后一項是最不重要的。

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));

主題數組是什么樣的。


 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',
          ]
    },
    ];

我不知道如何從最后3個主題問題數組中刪除4個開銷。 因此,我不會從三個最不重要的主題中抽取3 2 1,而不是抽取4(使平均數量達到20)。

我希望輸出總計為4 4 4 3 2 120。但是它只記錄2次六次。

實際上, 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);
  }
);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM