Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-07-2013, 01:15 PM   PM User | #1
m2244
Regular Coder

 
Join Date: Jun 2012
Posts: 129
Thanks: 1
Thanked 1 Time in 1 Post
m2244 is an unknown quantity at this point
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>
m2244 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:30 PM.


Advertisement
Log in to turn off these ads.