Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

how to random decide a question display or hide?

  • iscar
  • iscar's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
More
9 years 6 months ago #112626 by iscar
version 2.0,i need to design a survey with 10 questions, in which 9 questions are randomly display to participant.
it means participant must answer 1 question, final answer question number is from 1-10.
how to make it?
The topic has been locked.
  • iscar
  • iscar's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
More
8 years 11 months ago #119049 by iscar
tha's also a question in 2.05
The topic has been locked.
More
8 years 11 months ago #119053 by Bigred01
Note: After re-reading your post like 20 times I'm confident I don't really understand the question. But in my haste, I created an answer that I'm sure wont help you in any way but I really don't want to delete it as it may (hopefully) help someone somewhere... I should probably go to bed...

.........................

On the first page where they all answer the same question, add a multiple text question with 9 answers.

Add this script to the page..
Code:
var questionArr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];
 
shuffleArray(questionArr);
 
$(document).ready(function() {
    for(var i = 0;i<10;i++) {
        $('.text:eq('+i+')').val(questionArr[i]);
    }
});
 
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;
}

The example above has an array of 16 numbers. These numbers represent 16 potential question we might ask the respondent. The script randomizes the array of numbers, then stores the first 9 random numbers in the 9 text fields you added. You can then use relevance on each question to show if any of the 9 text boxes contains the number that represents that question.
The following user(s) said Thank You: iscar
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
8 years 11 months ago #119063 by tpartner
Hmm...I seem to recognize that code...

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
More
8 years 11 months ago #119088 by Bigred01
Ye Old Fisher Yates shuffle (copied from a stackoverflow post)
The topic has been locked.
  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
8 years 11 months ago #119092 by holch
Well, I am also not quite sure how to understand the question.

I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
8 years 11 months ago - 8 years 11 months ago #119094 by tpartner
I think that Bigred has put his finger on it, although my approach would be slightly different.

Let's say, for a simpler example, that you wanted to randomly display 4 out of a set of 10 questions.

Somewhere preceding the set of 10, I would place a multiple-short-text question with 10 sub-questions - one for each of the set.

In that multiple-short-text, I place the script below which shuffles an array and populates a random 4 of the sub-questions with "1". Then relevance is used to only show the questions in the set of 10 that correspond to sub-questions that equal 1.

Code:
<script type="text/javascript" charset="utf-8">  
  $(document).ready(function() { 
 
    // Hide this question
    $('#question{QID}').hide();
 
    // An array defining total number of questions in the set and number to be shown
    // In this case, 4 of 10 will be shown
    var setArray = [1, 1, 1, 1, 0, 0, 0, 0, 0, 0];
 
    // Shuffle the array
    setArray =  $.shuffle(setArray);
 
    // Loop Through the array and populate the sub-questions    
    $(setArray).each(function(i, value) {
      if($('#question{QID} input.text:eq('+i+')').val() == '') {
        $('#question{QID} input.text:eq('+i+')').val(value);
      }
    });
  });  
 
  // A function to shuffle arrays
  (function($){
    $.fn.shuffle = function() {
      return this.each(function(){
        var items = $(this).children();
        return (items.length)
          ? $(this).html($.shuffle(items))
          : this;
      });
    }
 
    $.shuffle = function(arr) {
      for(
        var j, x, i = arr.length; i;
        j = parseInt(Math.random() * i),
        x = arr[--i], arr[i] = arr[j], arr[j] = x
      );
      return arr;
    }
  })(jQuery);    
</script>





A sample survey:

File Attachment:

File Name: limesurvey...74-2.lss
File Size:44 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 8 years 11 months ago by tpartner.
The following user(s) said Thank You: iscar
The topic has been locked.
  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
8 years 11 months ago #119095 by holch
Works great Tony.

However, somehow the question still shows upon loading for the split of a second.

Is there any way to avoid this behaviour?

I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

The topic has been locked.
More
8 years 11 months ago #119096 by Bigred01
Hiding it with css would prevent that.

I like seeing your js posts Tpartner. Always learn something.
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
8 years 11 months ago #119140 by DenisChenu
Replied by DenisChenu on topic how to random decide a question display or hide?

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose