Hi all, Sorry to bother u all, I think i previously post about the quiz script, and i read and research abit here and there,

anyway i manage to write some but found some difficulties,especially about random generator..

I have this several 'set' of question and answer, and i like to view'em random
(e.g: refresh---> What is 1+1=?...with option n aswers, refresh---> What is 1+1=?...with option n aswers, refresh---> What is

1+1=?...with option n aswers, ...etc)

questionarray[0]  ='What is 1+1=?.' 
answer[0]  ='1' 
Adistract[0]  ='2' 
Bdistract[0]  ='22' 
Cdistract[0]  ='11' 
Ddistract[0]  ='0'  
questionarray[1]  ='what is the color of the sky?.' 
answer[1]  ='C' 
Adistract[1]  ='red' 
Bdistract[1]  ='yellow' 
Cdistract[1]  ='blue' 
Ddistract[1]  ='purple'  
questionarray[2]  ='cow is =?.' 
answer[0]  ='D' 
Adistract[0]  ='Tank' 
Bdistract[0]  ='President' 
Cdistract[0]  ='Weapon' 
Ddistract[0]  ='Animal'  
questionarray[3]  ='What is 2+2=?.' 
answer[0]  ='A' 
Adistract[0]  ='4' 
Bdistract[0]  ='22' 
Cdistract[0]  ='11' 
Ddistract[0]  ='0'  .....etc

and also if any of master n gurus here have better suggestion, both on the question array and get (viewing method on browser),

i'll be very thankful.
Anyway, thanks in advance.

Best Regards

~M~

Dani AI

Generated

— a practical, maintainable approach is to stop keeping parallel arrays and treat each question as one object, then randomize the question pool with a proper shuffle. pointed to randomness; the important follow-ups are (a) avoid the Math.round trick shown in the thread (it produces bias and can give out-of-range indices), and (b) use an unbiased shuffle so every ordering is equally likely.

A compact data shape and an unbiased shuffle look like this:

let pool = [
  { id: 1, text: "Question text", choices: ["A","B","C","D"], correctIndex: 0 },
  { id: 2, text: "Another question", choices: ["X","Y","Z","W"], correctIndex: 2 }
];
function shuffle(array) {
  for (let i = array.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [array[i], array[j]] = [array[j], array[i]];
  }
}

Shuffle the pool, then take the first N items for the quiz. To also shuffle each question's choices while keeping the correct answer tracked, map choices to indexed pairs, shuffle the pairs, then rebuild choices and recompute the correctIndex.

Avoid using Math.round(n * Math.random()) because the edge buckets are smaller/larger and the distribution is not uniform; use Math.floor(Math.random() * range) for single indices or the Fisher–Yates method above for full-array shuffling. For background on the algorithm and randomness in JS see Math.random() on MDN and the Fisher–Yates shuffle.

Notes: persist a shuffled order in localStorage if avoiding repeat questions across reloads is needed, and validate answers server-side if the quiz must be tamper-resistant.

Recommended Answers

All 3 Replies

along with the article on should get you going. Even though the article uses C/C++, adapting the algorithm to Javascript shouldn't be a big problem.

Thanks for the reply...

Also i've added change in the code (the last one is kinda wrong), here's the 'new' code

questionarray[0]  ='What is 1+1=?.' 
answer[0]  ='1' 
Adistract[0]  ='2' 
Bdistract[0]  ='22' 
Cdistract[0]  ='11' 
Ddistract[0]  ='0'  
questionarray[1]  ='what is the color of the sky?.' 
answer[1]  ='C' 
Adistract[1]  ='red' 
Bdistract[1]  ='yellow' 
Cdistract[1]  ='blue' 
Ddistract[1]  ='purple'  
questionarray[2]  ='cow is =?.' 
answer[2]  ='D' 
Adistract[2]  ='Tank' 
Bdistract[2]  ='President' 
Cdistract[2]  ='Weapon' 
Ddistract[2]  ='Animal'  
questionarray[3]  ='What is 2+2=?.' 
answer[3]  ='A' 
Adistract[3]  ='4' 
Bdistract[3]  ='22' 
Cdistract[3]  ='11' 
Ddistract[3]  ='0'  .....etc

i know this can be done by

var i = Math.round(6*Math.random());
document.write(stemarray[i]);

Thanks

Post a code which can be run / tested by us. Posting incomplete snippets from here and there won't help. Anyways, what is the problem you are facing right now? Does it work, if not, have you taken a look at the Error Console of Firefox (Tools -> Error Console)? Does it show any errors? If it works but not the way you wanted, what is the expected behavior?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.