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

标签 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"
      }
    ];

最佳答案

您可以像这样更改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/

相关文章:

javascript - 在没有 NodeJs 的客户端上使用 Swagger 和 javascript

javascript - 如何通过在 typescript 中调用函数来关闭bootstrap 4模式

javascript - 对 Javascript 数组中的数组中的元素进行排序

javascript - 插件 Jquery Validation 的错误信息没有正确显示

javascript - 无法使用 $.post() 发布 javascript 对象

javascript - 更改就绪处理程序上的 jQuery UI Accordion

javascript - php 脚本上没有显示 JSON 数据

javascript - 使用 Mongoose 更新数组中的特定对象时出错

javascript - IE 中的 document.getElementById

javascript - 获取元素内容的高度