Generating random aswers data for multiple choice tests form the question/ answer pairs.

Generating random aswers data for multiple choice tests form the question/ answer pairs.

Hello

i would like to use the following list as a template for generating
multiple choice tests.

  1. <ol >
  2. <li >
  3. <p>QUESTION</p>
  4. <ul id="ula">
  5. <li class="correct">CORRECT ANSWER</li>
  6. <li  class='inc1'>incorrect</li>
  7. <li class='inc2'>incorrect</li>
  8. <li  class='inc3'>incorrect</li>
  9. </ul>
  10. </li>
  11. </ol>

I store  test data the following way:
  1. <div id='cont' style="display:none">
  2. <span ><h3 class="q" id="q1">question1</h3><h4 class="a" id="a1">answer1</h4></span>
  3. <span ><h3 class="q" id="q2">question2</h3><h4 class="a" id="a2">answer2</h4></span>
  4. <span ><h3 class="q" id="q3">question3</h3><h4 class="a" id="a3">answer3</h4></span>
  5. <span ><h3 class="q" id="q4">question4</h3><h4 class="a" id="a4">answer4</h4></span>
  6. .....
  7. .....
  8. <span ><h3 class="q" id="q100">question100</h3><h4 class="a" id="a100">answer100</h4></span>
  9. </div>


 I would like to take data from the answers and generate some extra ones for each question  .

I was trying to add function that draw N elements from class="a"  and append them to the list as incorrect answers. The selection range should of course exclude the correct answer to be appended. The newly added elements must differ between themselves .


Currently  i`m using below functions to generate data from each span and add it one by one  to the template :
 
  1.  <script>
  2. var curIndex = 1;
  3. function changedata(index) {
  4.       curIndex = parseInt(index);
  5. $(".question,.correct") .empty()
  6. $(".inc3,.inc2,.inc1") .empty()
  7.  $('#q' + index ) .clone() .appendTo (".question")
  8.  $('#a' + index) .clone() .appendTo (".correct")
  9. $("h4:random")  .clone() .appendTo (".inc1")
  10. $("h4:random") .clone() .appendTo (".inc2")
  11. $("h4:random") .clone() .appendTo (".inc3")
  12. $('#ula').shuffle();
  13.  
  14. }
  15. function backward()  {
  16.       changedata(curIndex - 1);
  17. };
  18. function forward () {
  19.      changedata(curIndex + 1);
  20. };
  21.  </script>
Above code use shuffle plugin http://yelotofu.com/labs/jquery/snippets/shuffle/demo.html to mix the elements .

And random filter http://blog.mastykarz.nl/jquery-random-filter/ for making selection:

  1. <script>
  2. $(document).ready(function() {
  3.   
  4.   jQuery.jQueryRandom = 0;
  5. jQuery.extend(jQuery.expr[":"],
  6. {
  7.     random: function(a, i, m, r) {
  8.         if (i == 0) {
  9.             jQuery.jQueryRandom = Math.floor(Math.random() * r.length);
  10.         };
  11.         return i == jQuery.jQueryRandom;
  12.     }
  13. });
  14.     }
  15.    
  16.     )
  17.     </script>
 
 However, in the current form the function sometimes generates  identical answers.
 How to generate them without repetitions?
 Maybe there is some better way to insert random answers into template?

I was also thinking of another method to achieve my goal:
First use a function to generate the extra answers at once for the whole set.
Next use the cycle plugin to display  the content.

Thank You  in Advance