I've been attempting to randomize these quiz questions using the math random function, but my lack of experience is hindering my progress. How can I effectively randomize these questions for the quiz?
Is there a straightforward method to convert the questions into an array and then shuffle them for display on the browser? I have only included two questions in this example for simplicity, but I may include more.
var quiztitle = "Computer Hardware: Visual Quiz!";
/**
* Enter your question details here. Make sure the correct answer string matches exactly
* with the correct choice as it performs string matching (case sensitive).
*
*/
var quiz = [{
"question": "Q1: What is the name of this game?",
"image": "https://lh3.googleusercontent.com/N6XB3KXpnQ4OAz5jDJi74XM8dvwfJxiwrObSLpd7hdy3HzCxW-cZY0Mu5hJORtjeqa0=h900",
"choices":[
"Minecraft",
"Call of Duty",
"Doom",
"Toca Boca"
],
"correct": "Minecraft",
"explanation": "It is a picture of zombies in Minecraft!",
}, {
"question": "Q2: What game is this?",
"image": "https://assets.vg247.com/current//2015/03/crossy_road_header_1.jpg",
"choices": [
"Run sackboy",
"Call of Duty",
"Minecraft",
"Crossy road"
],
"correct": "Crossy road",
"explanation": "A picture of the signature chicken running across the road",
},
];
/**** Do not edit below this line *****/
var currentquestion = 0,
score = 0,
submt = true,
picked;
jQuery(document).ready(function($) {
/**
* Function to encode HTML for alt tags and attributes to prevent messy data inside tag attributes.
*/
function htmlEncode(value) {
return $(document.createElement('div')).text(value).html();
}
/**
* Add individual choices for each question to the ul#choice-block
*
* @param {choices} array The choices from each question
*/
function addChoices(choices) {
if (typeof choices !== "undefined" && $.type(choices) == "array") {
$('#choice-block').empty();
for (var i = 0; i < choices.length; i++) {
$(document.createElement('li')).addClass('choice choice-box').attr('data-index', i).text(choices[i]).appendTo('#choice-block');
}
}
}
// Other functions like nextQuestion, processQuestion, setupButtons, endQuiz and init follow...
});