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

时间:2016-11-20 22:18:56

标签: javascript arrays multidimensional-array random

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

如何在此测验中随机化问题的顺序?以下是工作样本:

https://jsfiddle.net/yhosftnt/

1 个答案:

答案 0 :(得分:0)

使用随机数(demo)对其进行排序:

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

[].concat(questions)创建一个新数组,因此不会更改原始数组。