简体   繁体   中英

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/

Sort them by using random numbers ( 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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