Okay, I'm obviously an extreme newbie, so be gentle. As I'm learning Javascript, I'm creating a quiz to better help me retain and practice the information. The following is a sample of my code so far(the actual array has been shortened for this question).
I works fine for what it is at this point in my learning progress, except that I can't seem to use Math.floor(Math.random()) to create a non-linear q & a experience.
var qAndA = [["What starts a variable?", "var"],
["What creates a popup with text and an OK button?", "alert()"],
["What is the sign for a modulo?", "%"]];
function askQuestion(qAndA) {
var answer = prompt(qAndA[0], " ");
if (answer === qAndA[1]) {
alert("yes");
} else {
alert("No, the answer is " + qAndA[1]);
}
}
for (i = 0; i < qAndA.length; i++) {
askQuestion(qAndA[i]);
}
I've looked at all the potential answers here and elsewhere, but nothing addresses this speicific point.
Can anyone out there help me?
Easiest way to select a random element in an array:
var randomIndex = Math.floor(Math.random() * qAndA.length)
var randomQuestion = qAndA[randomIndex]
Now put that in a loop:
var questionsToAsk = qAndA.length
for (i = 0; i < questionsToAsk; i++) {
var randomIndex = Math.floor(Math.random() * qAndA.length)
var randomQuestion = qAndA[randomIndex]
askQuestion(randomQuestion);
}