简体   繁体   English

如何添加一些随机性来填充此列表?

[英]How can I add some randomness to populating this list?

I'm trying to populate a list of answers in a quiz. 我正在尝试在测验中填充答案列表。 The correct answer is always the a0 in the data object. 正确的答案始终是数据对象中的a0 How can I make it so the first answer isn't always the correct one? 我如何才能做到,所以第一个答案并不总是正确的? A hint would be much appreciated! 提示将不胜感激!

JS: JS:

data = [
        {"q":"How much?", "a0":"20%", "a1": "1%", "a2": "10%", "a3": "5%"},
        {"q":"What", "a0":"Ball", "a1": "Stone", "a2": "Bierd", "a3": "Carl"},
        {"q":"When?", "a0":"1999", "a1": "2000", "a2": "2001", "a3": "2002"}
        ];

function addData() {

var ids =['a','b','c','d'];
for (var j=0; j < ids.length; j++) {

    //Populate answers into list
    document.getElementById(ids[j]).innerHTML = data[q]['a'+j];

    //Add class 'correct' to the li with the right answer (always a0 in the data object)
    if (j==0) {
    document.getElementById(ids[j]).setAttribute('class', 'correct');
    }
};
}

HTML: HTML:

<ul class="answers_quiz">
<li id="a"></li>
<li id="b"></li>
<li id="c"></li>
<li id="d"></li>
</ul>

Just sort the array using random numbers: 只需使用随机数对数组进行排序:

var ids =['a','b','c','d'];
ids.sort(function(x, y) {
    return parseInt(Math.random() * ids.length);
});
for (var j=0; j < ids.length; j++) {
    //.....
}

Live test case . 现场测试用例 (of the random sort) (随机排序)

You can generate a Random number and use it as a position of your array, always verifying the lenght of it and the number you got, of course. 您可以生成一个随机数并将其用作数组的位置,当然,请始终验证其长度和获得的数字。

You can generate a random number using something like that: 您可以使用以下方式生成随机数:

var randomnumber = Math.random();

you can sort array randomly as well as in sorting order. 您可以对数组进行随机排序,也可以按排序顺序进行排序。

Your array is String array and it doesnt matter that your answer comes in alphbetic order so you can try to sort it alphbetically. 您的数组是字符串数组,您的答案按字母顺序排列并不重要,因此您可以尝试按字母顺序对其进行排序。

following example is to sort string array. 下面的示例是对字符串数组进行排序。

//Sort alphabetically and ascending:
var myarray=["Bob", "Bully", "Amy"]
myarray.sort() //Array now becomes ["Amy", "Bob", "Bully"]

//Sort alphabetically and descending:
var myarray=["Bob", "Bully", "Amy"]
myarray.sort()
myarray.reverse() //Array now becomes ["Bully", "Bob", "Amy"]

refer http://www.javascriptkit.com/javatutors/arraysort.shtml 请参阅http://www.javascriptkit.com/javatutors/arraysort.shtml

Now if you want to sort it by its index you can use following. 现在,如果要按索引对其进行排序,则可以使用以下命令。

function randomSort(a,b) {
    return( parseInt( Math.random()*10 ) %2 );
}

var arlene = new Array( 1, 2, 3, 4, 5 );
alert( arlene.sort(randomSort).toString() );

refer http://freewebdesigntutorials.com/javaScriptTutorials/jsArrayObject/randomizeArrayElements.htm 请参阅http://freewebdesigntutorials.com/javaScriptTutorials/jsArrayObject/randomizeArrayElements.htm

This keeps the order of answers, changing just which is first. 这样可以保持答案的顺序,只是先更改哪个。

Make random integer before the for loop: var offset=Math.floor(Math.random()*ids.length) , then first thing in loop do var idx='a'+((j+offset)%ids.length) . 在for循环之前做一个随机整数: var offset=Math.floor(Math.random()*ids.length) ,然后循环中的第一件事就是做var idx='a'+((j+offset)%ids.length) Instead of data[q]['a'+j] use data[q][idx] , and set class to correct when idx=="a0" instead of j==0 . 代替data[q]['a'+j]使用data[q][idx] ,并设置类以在idx=="a0"而不是j==0时更正。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM