代码之家  ›  专栏  ›  技术社区  ›  Millhorn

如何显示此JavaScript输出中特定数量的问题?

  •  0
  • Millhorn  · 技术社区  · 6 年前

    这是功能齐全的 Codepen 供参考。

    有没有快速解决办法?

    var $progressValue = 0;
    var resultList=[];
    
    
    const quizdata=[
          {
            question:"Characterized by skill at understanding and profiting by circumstances",
            options:["Prescient", "Analyst", "Diminution", "Shrewd"],
            answer:"Shrewd"
          },
          {
            question:"To refuse to acknowledge as one's own or as connected with oneself",
            options:["Prevalent", "Disown", "Squalid", "Employee"],
            answer:"Disown"
          },
          {
            question:"Not having the abilities desired or necessary for any purpose",
            options:["Incompetent", "Impoverish", "Coxswain", "Devious"],
            answer:"Incompetent"
          },
          {
            question:"Lizard that changes color in different situations",
            options:["Scruple", "Depredation", "Chameleon", "Whimsical"],
            answer:"Chameleon"
          },
          {
            question:"Having the title of an office without the obligations",
            options:["Reciprocal", "Unsullied", "Titular", "Inflated"],
            answer:"Titular"
          }
        ];
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Marcelo Myara    6 年前

    您可以这样更改generateQuestions函数:

    /*** Return shuffled question ***/
    function generateQuestions(qtty = -1){
      var questions=shuffleArray(quizdata);    
      if (qtty > 0) questions = questions.slice(0, qtty);
      return questions;
    }
    

    这将使它收到一个可选参数,告诉它返回特定数量的问题。

    你可以做的一件事就是在一个隐藏的输入中包含问题的数量。就像这样:

    <input type="hidden" id="QttyQuestions" value="3" />
    

    然后,寻找 generateQuestions() questions=generateQuestions(); “在您的代码上),并将其更改为:

    questions=generateQuestions($('#QttyQuestions').val());