在以下脚本中,我如何显示 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());
关于javascript - 如何从此 JavaScript 输出中显示特定数量的问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52105194/