how can i get items in a named array randomly
i want to display questions and below i want to put a textbox for answer. for eg. What is the capital of India.. When the page loads at first time this question appears.. when loads second time another question should appear randomly how to write.. p开发者_如何学编程lease help me
If you have an array of questions, var questions = ["asdf", "sdfg", ...];
then you can get random ones by:
questions[Math.floor(Math.random() * questions.length)];
Edit: For associative arrays...
Just create a normal array from it:
var questions = [];
for (var i in assocArr) {
if (assocArr.hasOwnProperty(i)) questions.push(assocArr[i]);
}
And then use the above method.
精彩评论