diff --git a/main/template/default/exercise/submit.js.tpl b/main/template/default/exercise/submit.js.tpl index 6c361c8c3a..c373716d07 100644 --- a/main/template/default/exercise/submit.js.tpl +++ b/main/template/default/exercise/submit.js.tpl @@ -222,33 +222,34 @@ jsPlumb.ready(function () { } }); -$(function () { - DraggableAnswer.init( - $(".exercise-draggable-answer"), - $(".droppable") - ); +function shuffleArray(array) { + for (var i = array.length - 1; i > 0; i--) { + var j = Math.floor(Math.random() * (i + 1)); + var temp = array[i]; + array[i] = array[j]; + array[j] = temp; + } +} +$(function () { // if shuffle answers if ('{{ shuffle_answers }}' == '1') { $('.exercise-draggable-answer').each(function(){ // get current ul var $ul = $(this); - // get array of list items in current ul - var $liArr = $ul.children('li'); - // sort array of list items in current ul randomly - $liArr.sort(function(a,b){ - // Get a random number between 0 and 10 - var temp = parseInt( Math.random()*100 ); - // Get 1 or 0, whether temp is odd or even - var isOddOrEven = temp%2; - // Get +1 or -1, whether temp greater or smaller than 5 - var isPosOrNeg = temp>5 ? 1 : -1; - // Return -1, 0, or +1 - return( isOddOrEven*isPosOrNeg ); - }) - // append list items to ul - .appendTo($ul); + var li = $ul.find('.touch-items'); + var liContents = []; + li.each(function() { + liContents.push($(this)); + }); + shuffleArray(liContents); + $ul.empty().html(liContents); }); } + + DraggableAnswer.init( + $(".exercise-draggable-answer"), + $(".droppable") + ); });