CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript frameworks (http://www.codingforums.com/forumdisplay.php?f=62)
-   -   Can these answers be randomized easily (http://www.codingforums.com/showthread.php?t=287252)

m2244 02-07-2013 01:15 PM

Can these answers be randomized easily
 
Hello,

I am hoping that I can easily randomize the order of these answers without redoing a lot of code. The problem is that I am putting each question from an XML file into an array (this block of XML includes the answers). In hind sight it would have been better if I had put each anser into it's own array. In the jQuery '.each' loop below I was hoping I could do something to randomize the list of answers. I am including the XML structure.

Any help would be greatly appreciated.

Code:

//***** Loop through the array, display each answer, store the correct answer for comparison later *********/
                $(questionArr[questionIndex]).find('answer').each(function(){
                        $("#kc_answers").append('<li><label><input type="radio" name="example1" /><span class="kc_answer_span">' + $(this).find('aText').text() + '</span></label></li>');
                        if($(this).attr('correct') == 'true')
                        {
                                correctAnswer = $(this).find('aText').text();
                        }
                });

Code:

$("#kc_directions").html($(xml).find('directions').text());
       
        //***** Store each question block seperately in the question array. ********/
        $(xml).find('question').each(function(){
                questionArr.push($(this));
        });
       
        //***** Randomize the questions. ********/
        for(var j, x, i = questionArr.length; i; j = parseInt(Math.random() * i), x = questionArr[--i], questionArr[i] = questionArr[j], questionArr[j] = x);

Code:

                <question>
            <qText>(U) What is the meaning of this?</qText>
                        <answers random="true">
                                <answer correct="true">
                                        <aText>Who knows</aText>
                                        <feedback>You're incredible!</feedback>
                                </answer>
                                <answer correct="false">
                                        <aText>You know</aText>
                                        <feedback>Wrong</feedback>
                                </answer>
                                <answer correct="false">
                                        <aText>C</aText>
                                        <feedback>Wrong</feedback>
                                </answer>
                        </answers>
                </question>



All times are GMT +1. The time now is 03:24 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.