如何为数组随机化这些问题
How to randomize these questions for arrays?
var quiz = [
{
"question" : "Q1: Who is the best scientist?",
"choices" : [
"Sir Isaac Newton",
"Albert Einstein",
"Nicolaus Copernicus",
"Ralph Waldo Emmerson"
],
"correct" : "Albert Einstein",
"explanation" : "Albert Einstein drafted the special theory of relativity.",
},
{
"question" : "Q2: Who looks better?",
"choices" : [
"Thomas",
"Dwight Howard",
"Benjamin Parker",
"Jeremy Lincoln"
],
"correct" : "Benjamin Parker",
"explanation" : "He has better features to start with",
},
{
"question" : "Q3: What event began on December 25, 1990?",
"choices" : [
"Christmas",
"Chinese New Year",
"American Civil War began",
"Declaration of Independence"
],
"correct" : "Christmas",
"explanation" : "Duh, Christmas? I think this needs no future explaination",
},
];
function loadQuestion(){
//set temporary variable for creating radio buttons
var radioButton;
//clear out radio buttons from previous question
document.getElementById('content').innerHTML = "";
//loop through choices, and create radio buttons
for(var i=0; i < quiz[currentQuestion]["choices"].length; i++){
radioButton = document.createElement('input');
radioButton.type = 'radio';
radioButton.name = 'quiz';
radioButton.id = 'choice'+ (i+1);
radioButton.value = quiz[currentQuestion]["choices"][i];
//create label tag, which hold the actual text of the choices
var label = document.createElement('label');
label.setAttribute('for','choice'+ (i+1));
label.innerHTML = quiz[currentQuestion]["choices"][i];
//create a <br> tag to separate options
var br = document.createElement('br');
//attach them to content. Attach br tag, then label, then radio button
document.getElementById('content').insertBefore(br);
document.getElementById('content').insertBefore(label, br);
document.getElementById('content').insertBefore(radioButton, label);
}
var currentQuestion = Math.floor(Math.random() * myArray.length);
创建一个随机数生成器:
var i = quiz.length;
var n = Math.floor((Math.random() * i));
var question = quiz[n]
来源:
Javascript随机,
Array.length .
下面是所有quiz
项的循环,并使用"How to randomize (shuffle) a JavaScript array"解决方案来随机化选项数组:
var quiz = [{"question": "Q1: Who is the best scientist?","choices" : ["Sir Isaac Newton","Albert Einstein","Nicolaus Copernicus","Ralph Waldo Emmerson"],"correct" : "Albert Einstein","explanation" : "Albert Einstein drafted the special theory of relativity.",},{"question": "Q2: Who looks better?","choices" : ["Thomas","Dwight Howard","Benjamin Parker","Jeremy Lincoln"],"correct" : "Benjamin Parker","explanation" : "He has better features to start with",},{"question": "Q3: What event began on December 25, 1990?","choices" : ["Christmas","Chinese NewYear","American Civil War began","Declaration of Independence"],"correct" : "Christmas","explanation" : "Duh, Christmas? I think this needs no future explaination"}],
shuffleArray = function (array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1)),
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
},
loadQuestions = function (quiz) {
quiz.forEach(function(item) {
var choices = shuffleArray(item.choices);
console.log(item.question);
choices.forEach(function(choice) {
console.log('-', choice);
});
});
}
;
// Load questions
loadQuestions(quiz);
相关文章:
- $(document).height()在刷新时随机化值(Safari 5.1.10)
- 将键盘使用与 Qualtrics 中的答案随机化相结合
- 我怎样才能随机化 HTML 元素的颜色属性
- 通过AJAX调用PHP随机化器的输出
- 如何在JS中随机化backgroundPosition而不使用鼠标移动来制作万花筒的动画
- 使用Javascript对RGBA颜色进行光标控制的形状随机化
- 如何在一个随机化的有序列表中随机化多个无序列表
- 如何使用JavaScript随机化有序列表
- 使用JavaScript随机化html标记
- 可以'我的单词随机化器脚本不能正常循环
- 如果语句是假的 JavaScript,则再次随机化数字
- 函数随机化 - 我可以让它错过一个页面并且该页面上只有一个图像吗?可湿性粉剂
- 随机化网页上的图像和颜色对
- 随机侮辱生成器(随机化结果)
- Javascript:通过在while循环内推送/拼接来随机化数组
- 有关随机化脚本的问题
- Javascript-随机化Prim's算法问题随机化Prim's算法
- 如何为数组随机化这些问题
- 只是一个简单的问题??关于随机化这个幻灯片
- jQuery图像谜题:图像随机化问题