简体   繁体   English

如何随机化这些问题的数组?

[英]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);
        }

create a random number generator like this: 创建一个像这样的随机数生成器:

var i = quiz.length; var n = Math.floor((Math.random() * i)); var question = quiz[n]

source: 资源:
Javascript random , Javascript random
Array.length . Array.length

var currentQuestion = Math.floor(Math.random() * myArray.length);

Here is a loop over all the quiz items and using the " How to randomize (shuffle) a JavaScript array " solution to randomize the array of choices: 这是所有quiz项目的循环,并使用“ 如何随机化(随机播放)JavaScript数组 ”解决方案来随机化选择的数组:

 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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在多维数组中随机化测验问题顺序? - How to randomize quiz questions order in multidimensional array? jQuery-如何在数组数组中随机化? - jQuery - How to randomize within an array of arrays? 如何随机化两种不同的JavaScript数组 - How to randomize this kind of two different javascript arrays 将对象随机化并分成2个数组 - Randomize and split object into 2 arrays 每次用户使用android中的json数组玩游戏时如何随机化测验中的问题 - how to randomize questions in the quiz every time the user play using json array in android 如何使用 Math.random() 函数来随机化一系列测试问题? - How can I use the Math.random() function to randomize a series of test questions? 如何集成随机数生成器代码以随机化编码中的问题? - How do I integrate the random number generator code to randomize the questions in my coding? 将3个对象阵列合并为一个并随机化 - Combine 3 arrays of objects into one and Randomize 使用“.push”从 arrays 生成 arrays 时,如何更好地“随机化”我的结果并控制重复项? - When generating arrays from arrays using “.push”, how can I better “randomize” my results and control for duplicates? 我应该如何 go 关于使用 for 循环将 arrays 连接到另一个数组并生成结果? - How should I go about using a for loop to randomize arrays concat into another array and generate the outcome?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM