我正在尝试使用react进行测验,该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
并将问题和选项作为props
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 和 name 我应该给?
谢谢
要回答您的问题,您可以使用随机函数。只需在您的功能中添加以下代码行:
let ind= Math.floor(Math.random()*Math.floor(quiz.length));
console.log(ind);
console.log(quiz [ind] .question(
然后您可以导出两个变量索引和测验。然后就像在MCQ功能中使用一样使用
Quiz[index].options [0]
我希望你得到答案。希望您得到答案。