- Posts: 55
- Thank you received: 3
Ask the community, share ideas, and connect with other LimeSurvey users!
Please Log in to join the conversation.
<script type="text/javascript" data-author="Tony Partner"> $(document).on('ready pjax:scriptcomplete',function(){ 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; } return array; } //Identify this question var thisQuestion = $('#question{QID}'); var thisAnswerList = $('tr.answers-list:eq(0)', thisQuestion).parent(); // Special codes appear after var afterCode=4; // Fill the array with "normal" codes var array1=[1,3,4,5,7]; // Fill the array with "special" codes var array2=[2,6]; shuffleArray(array1); array2=array2.concat(array1.slice(afterCode)); array1=array1.slice(0,afterCode); shuffleArray(array2); array1=array1.concat(array2); // Loop through the answer codes $.each(array1, function(i, val) { // Move the answer item $(thisAnswerList).append($('tr.answers-list[id$="X{QID}'+val+'"]', thisQuestion)); }); }); </script>
Please Log in to join the conversation.
Please Log in to join the conversation.
Please Log in to join the conversation.
<script type="text/javascript" data-author="Tony Partner"> $(document).on('ready pjax:scriptcomplete',function(){ 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; } return array; } // Merge function function mergeArrays(array1, array2) { var mergedArray = []; var maxLength = Math.max(array1.length, array2.length); for (var i = 0; i < maxLength; i++) { if (i < array2.length) { mergedArray.push(array2[i]); } if (i < array1.length) { mergedArray.push(array1[i]); } } return mergedArray; } //Identify this question var thisQuestion = $('#question{QID}'); var thisAnswerList = $('tr.answers-list:eq(0)', thisQuestion).parent(); var thisTable = $('table.subquestion-list:eq(0)', thisQuestion); // Special codes appear after var afterCode = 4; //Array for K Codes var array_k = ["b","d","g","h","i","j","l"]; //Array for N Codes var array_n = ["c","e","f","k","m","n"]; //Array for Special K Codes var array_sk = []; //Array for Special N codes var array_sn = ["a"]; //Array for super special codes var array_ss = ["p"]; // Shuffle order of "normal" arrays shuffleArray(array_k); shuffleArray(array_n); // Slice "normal" arrays and merge second part to special codes array_sk = array_sk.concat(array_k.slice(afterCode)); array_sn = array_sn.concat(array_n.slice(afterCode)); // Save only first part of "normal" arrays array_k = array_k.slice(0, afterCode); array_n = array_n.slice(0, afterCode); // Shuffle special arrays shuffleArray(array_sk); shuffleArray(array_sn); shuffleArray(array_ss); // Combine normal and special arrays array_k = array_k.concat(array_sk); array_n = array_n.concat(array_sn); // Merge arrays in order n,k,n,k... var array_res = mergeArrays(array_k, array_n); //Add super special array to end of result array array_res = array_res.concat(array_ss); // Loop through the answer codes $.each(array_res, function(i, val) { // Move the answer item $(thisAnswerList).append($('tr.answers-list[id$="X{QID}'+val+'"]', thisQuestion)); }); // Fix up the row classes var rowClass = 1; $('tr.answers-list', thisTable).each(function(i) { $(this).addClass('array'+(2-(i%2))); }); }); </script>
Please Log in to join the conversation.
Please Log in to join the conversation.
Sure:Sample survey?
Please Log in to join the conversation.
<script type="text/javascript"> $(document).on('ready pjax:scriptcomplete',function(){ 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; } return array; } // Merge function function mergeArrays(array1, array2) { var mergedArray = []; var maxLength = Math.max(array1.length, array2.length); for (var i = 0; i < maxLength; i++) { if (i < array2.length) { mergedArray.push(array2[i]); } if (i < array1.length) { mergedArray.push(array1[i]); } } return mergedArray; } //Identify this question var thisQuestion = $('#question{QID}'); var thisAnswerList = $('tr.answers-list:eq(0)', thisQuestion).parent(); var thisTable = $('table.subquestion-list:eq(0)', thisQuestion); // Special codes appear after var afterCode = 4; //Array for K Codes var array_k = ["b","d","g","h","i","j","l"]; //Array for N Codes var array_n = ["c","e","f","k","m","n"]; //Array for Special K Codes var array_sk = []; //Array for Special N codes var array_sn = ["a"]; //Array for super special codes var array_ss = ["p"]; // Shuffle order of "normal" arrays shuffleArray(array_k); shuffleArray(array_n); // Slice "normal" arrays and merge second part to special codes array_sk = array_sk.concat(array_k.slice(afterCode)); array_sn = array_sn.concat(array_n.slice(afterCode)); // Save only first part of "normal" arrays array_k = array_k.slice(0, afterCode); array_n = array_n.slice(0, afterCode); // Shuffle special arrays shuffleArray(array_sk); shuffleArray(array_sn); shuffleArray(array_ss); // Combine normal and special arrays array_k = array_k.concat(array_sk); array_n = array_n.concat(array_sn); // Merge arrays in order n,k,n,k... var array_res = mergeArrays(array_k, array_n); //Add super special array to end of result array array_res = array_res.concat(array_ss); // Loop through the answer codes $.each(array_res, function(i, val) { // Move the answer item $(thisAnswerList).append($('tr.answers-list[id$="X{QID}'+val+'"]', thisQuestion)); }); // Fix up the row background colours $('tr.answers-list', thisQuestion). each (function(i){ $(this).removeClass('ls-even ls-odd'); if(i % 2 == 0) { $(this).addClass('ls-even'); } else { $(this).addClass('ls-odd'); } }); }); </script>
Please Log in to join the conversation.
Please Log in to join the conversation.
Please Log in to join the conversation.
<script type="text/javascript" data-author="BBSR-SR5 & Tony Partner"> $(document).on('ready pjax:scriptcomplete',function(){ //Define Seeded Random function function splitmix32(a) { return function() { a |= 0; a = a + 0x9e3779b9 | 0; let t = a ^ a >>> 16; t = Math.imul(t, 0x21f0aaad); t = t ^ t >>> 15; t = Math.imul(t, 0x735a2d97); return ((t = t ^ t >>> 15) >>> 0) / 4294967296; }; } function shuffleArray(array) { const seed = Date.now(); const random = splitmix32(seed); for (let i = array.length - 1; i > 0; i--) { const j = Math.floor(random() * (i + 1)); [array[i], array[j]] = [array[j], array[i]]; } return array; } // Merge function function mergeArrays(array1, array2) { var mergedArray = []; var maxLength = Math.max(array1.length, array2.length); for (var i = 0; i < maxLength; i++) { if (i < array2.length) { mergedArray.push(array2[i]); } if (i < array1.length) { mergedArray.push(array1[i]); } } return mergedArray; } //Identify this question var thisQuestion = $('#question{QID}'); var thisAnswerList = $('tr.answers-list:eq(0)', thisQuestion).parent(); var thisTable = $('table.subquestion-list:eq(0)', thisQuestion); // Special codes appear after var afterCode = 4; //Array for K Codes var array_k = ["b","d","g","h","i","j","l"]; //Array for N Codes var array_n = ["c","e","f","k","m","n"]; //Array for Special K Codes var array_sk = []; //Array for Special N codes var array_sn = ["a"]; //Array for super special codes var array_ss = ["o"]; // Shuffle order of "normal" arrays shuffleArray(array_k); shuffleArray(array_n); // Slice "normal" arrays and merge second part to special codes array_sk = array_sk.concat(array_k.slice(afterCode)); array_sn = array_sn.concat(array_n.slice(afterCode)); // Save only first part of "normal" arrays array_k = array_k.slice(0, afterCode); array_n = array_n.slice(0, afterCode); // Shuffle special arrays shuffleArray(array_sk); shuffleArray(array_sn); shuffleArray(array_ss); // Combine normal and special arrays array_k = array_k.concat(array_sk); array_n = array_n.concat(array_sn); // Merge arrays in order n,k,n,k... var array_res = mergeArrays(array_k, array_n); //Add super special array to end of result array array_res = array_res.concat(array_ss); // Loop through the answer codes $.each(array_res, function(i, val) { // Move the answer item $(thisAnswerList).append($('tr.answers-list[id$="X{QID}'+val+'"]', thisQuestion)); }); // Fix up the row background colours $('tr.answers-list', thisQuestion). each (function(i){ $(this).removeClass('ls-even ls-odd'); if(i % 2 == 0) { $(this).addClass('ls-even'); } else { $(this).addClass('ls-odd'); } }); //alert(array_res); //console.log(array_res); }); </script>
Please Log in to join the conversation.
<script type="text/javascript"> $(document).on('ready pjax:scriptcomplete',function(){ 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; } return array; } // Merge function function mergeArrays(array1, array2) { var mergedArray = []; var maxLength = Math.max(array1.length, array2.length); for (var i = 0; i < maxLength; i++) { if (i < array2.length) { mergedArray.push(array2[i]); } if (i < array1.length) { mergedArray.push(array1[i]); } } return mergedArray; } // Identify some elements and define variables var thisQuestion = $('#question{QID}'); var thisAnswerList = $('tr.answers-list:eq(0)', thisQuestion).parent(); var thisTable = $('table.subquestion-list:eq(0)', thisQuestion); var orderQuestion = $(thisQuestion).nextAll('.text-short:eq(0)'); var orderInput = $(':text.form-control:eq(0)', orderQuestion); var array_res; var aOrder = []; // Previously set order if($.trim($(orderInput).val()) != '') { array_res = $.trim($(orderInput).val()).split(','); } // No previous order set else { // Special codes appear after var afterCode = 4; //Array for K Codes var array_k = ["b","d","g","h","i","j","l"]; //Array for N Codes var array_n = ["c","e","f","k","m","n"]; //Array for Special K Codes var array_sk = []; //Array for Special N codes var array_sn = ["a"]; //Array for super special codes var array_ss = ["p"]; // Shuffle order of "normal" arrays shuffleArray(array_k); shuffleArray(array_n); // Slice "normal" arrays and merge second part to special codes array_sk = array_sk.concat(array_k.slice(afterCode)); array_sn = array_sn.concat(array_n.slice(afterCode)); // Save only first part of "normal" arrays array_k = array_k.slice(0, afterCode); array_n = array_n.slice(0, afterCode); // Shuffle special arrays shuffleArray(array_sk); shuffleArray(array_sn); shuffleArray(array_ss); // Combine normal and special arrays array_k = array_k.concat(array_sk); array_n = array_n.concat(array_sn); // Merge arrays in order n,k,n,k... array_res = mergeArrays(array_k, array_n); //Add super special array to end of result array array_res = array_res.concat(array_ss); // Load the hidden order input $(orderInput).val(array_res); } // Loop through the answer codes $.each(array_res, function(i, val) { // Move the answer item $(thisAnswerList).append($('tr.answers-list[id$="X{QID}'+val+'"]', thisQuestion)); }); // Fix up the row background colours $('tr.answers-list', thisQuestion). each (function(i){ $(this).removeClass('ls-even ls-odd'); if(i % 2 == 0) { $(this).addClass('ls-even'); } else { $(this).addClass('ls-odd'); } }); }); </script>
Please Log in to join the conversation.