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.
- <ol >
- <li >
- <p>QUESTION</p>
- <ul id="ula">
- <li class="correct">CORRECT ANSWER</li>
- <li class='inc1'>incorrect</li>
- <li class='inc2'>incorrect</li>
- <li class='inc3'>incorrect</li>
- </ul>
- </li>
- </ol>
I store test data the following way:
- <div id='cont' style="display:none">
- <span ><h3 class="q" id="q1">question1</h3><h4 class="a" id="a1">answer1</h4></span>
- <span ><h3 class="q" id="q2">question2</h3><h4 class="a" id="a2">answer2</h4></span>
- <span ><h3 class="q" id="q3">question3</h3><h4 class="a" id="a3">answer3</h4></span>
- <span ><h3 class="q" id="q4">question4</h3><h4 class="a" id="a4">answer4</h4></span>
- .....
- .....
- <span ><h3 class="q" id="q100">question100</h3><h4 class="a" id="a100">answer100</h4></span>
- </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 :
- <script>
- var curIndex = 1;
- function changedata(index) {
- curIndex = parseInt(index);
- $(".question,.correct") .empty()
- $(".inc3,.inc2,.inc1") .empty()
- $('#q' + index ) .clone() .appendTo (".question")
- $('#a' + index) .clone() .appendTo (".correct")
- $("h4:random") .clone() .appendTo (".inc1")
- $("h4:random") .clone() .appendTo (".inc2")
- $("h4:random") .clone() .appendTo (".inc3")
- $('#ula').shuffle();
-
- }
- function backward() {
- changedata(curIndex - 1);
- };
- function forward () {
- changedata(curIndex + 1);
- };
- </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:
- <script>
- $(document).ready(function() {
-
- jQuery.jQueryRandom = 0;
- jQuery.extend(jQuery.expr[":"],
- {
- random: function(a, i, m, r) {
- if (i == 0) {
- jQuery.jQueryRandom = Math.floor(Math.random() * r.length);
- };
- return i == jQuery.jQueryRandom;
- }
- });
- }
-
- )
- </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