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

时间:2018-08-30 20:52:19

标签: javascript

在以下脚本中,如何显示5个问题中的特定数目。例如,我只想显示3个问题。

以下是功能全面的 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 个答案:

答案 0 :(得分:1)

您可以像这样更改generateQuestions函数:

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

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

您将不得不更改它的调用(位于第297行附近的$(document).ready()上)以传递所需数量的问题。

您可以做的一件事就是清楚地说明隐藏输入中的问题数量。像这样:

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

然后,寻找generateQuestions()调用(可以在代码中搜索“ questions=generateQuestions();”)并按如下所示进行更改:

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