[英]How can create next and previous button's to select questions in a quiz using an array to store the questions which are objects?
[英]How can I select random N number of questions from array containing many questions?
我正在尝试使用 React 进行测验,该测验显示来自 javascript 文件的随机 100 个问题。
这是javascript文件中的问题。
const quizQuestions = [ { question: "Grand Central Terminal, Park Avenue, New York is the world's", options: ["largest railway station", "highest railway station", "longest railway station", "None of the above"], answer: "largest railway station" }, { question: "Entomology is the science that studies", options: ["Behavior of human beings", "Insects", "The origin and history of technical and scientific terms", "The formation of rocks"], answer: "The origin and history of technical and scientific terms" }, { question: "Eritrea, which became the 182nd member of the UN in 1993, is in the continent of", options: ["Asia", "Africa", "Europe", "Australia"], answer: "Africa" }, { question: "Garampani sanctuary is located at", options: ["Junagarh, Gujarat", "Diphu, Assam", "Kohima, Nagaland", "Gangtok, Sikkim"], answer: "Diphu, Assam" }, { question: "Hitler party which came into power in 1933 is known as", options: ["Labour Party", "Nazi Party", "Ku-Klux-Klan","Democratic Party"], answer: "Nazi Party" } ] export default quizQuestions;
1. 我需要 javascript 代码从上面的文件中以随机顺序选择例如 3 个随机问题。 同样随机化选项而不重复问题
2. 我想通过调用组件MCQ
并将问题和选项作为道具传递来执行以下函数的次数与问题的MCQ
一样多
function MCQ(props) { return( <div> <div>{props.question}</div> <div> <input type="radio" name="answer" id=??? /><label for="???"> {props.options[0]}</label> <input type="radio" name="answer" id=??? /><label for="???"> {props.options[1]}</label> <input type="radio" name="answer" id=??? /><label for="???"> {props.options[2]}</label> <input type="radio" name="answer" id=??? /><label for="???"> {props.options[3]}</label> </div> </div> ) }
我应该给出什么ID和名字?
谢谢你
要回答您的问题,您可以使用随机函数。 只需在您的函数中添加以下代码行:
let ind= Math.floor(Math.random()*Math.floor(quiz.length));
console.log(ind);
控制台.日志(测验[ind].问题)
然后你可以导出两个变量 index 和 quizQuestion 。 然后像您在 MCQ 功能中使用的那样使用
Quiz[index].options [0]
我希望你能得到答案。 希望你能得到答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.