简体   繁体   English

如何在多维数组中随机化测验问题顺序?

[英]How to randomize quiz questions order in multidimensional array?

var questions =[
new Question ("Who is the president of Czech Republic?", ["Donald Trump", "Andrej Kiska", "Milos Zeman", "Angela Merkel"], "Milos Zeman"),
new Question ("The capital of Czech Republic is:", ["Brno", "Ostrava", "Hradec Kralove", "Prague"], "Prague"),
new Question ("Which food is not common Czech food?", ["Cevapcici", "Svickova", "Trdelnik", "Tatarak"], "Cevapcici")];
var quiz = new Quiz(questions);
populate();

How do I randomize the order of the questions in this quiz? 如何在本测验中将问题的顺序随机化? Here is the working sample: 这是工作示例:

https://jsfiddle.net/yhosftnt/ https://jsfiddle.net/yhosftnt/

Sort them by using random numbers ( demo ): 使用随机数( demo )对它们进行排序:

this.questions = [].concat(questions).sort(function() {
    return Math.random() - Math.random();
});

The [].concat(questions) creates a new array, so the original array won't be changed. [].concat(questions)创建一个新数组,因此不会更改原始数组。

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

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