簡體   English   中英

如何顯示此JavaScript輸出中特定數量的問題?

[英]How do I display a specific number of questions from this JavaScript output?

在以下腳本中,如何顯示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"
      }
    ];

您可以像這樣更改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());

暫無
暫無

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

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