Welcome to the LimeSurvey Community Forum

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

For Multiple Choice Questions, can the sub questions takes x out of y answer

  • etc4xx
  • etc4xx's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
4 years 1 month ago #193751 by etc4xx
Hello All

Need assistance to with regards to the Multiple choice type questions.

Can i select x numbers of subquestions, that is taken from a pool of y numbers of subquestion?

It is similar with the issues in the link provided, but instead of questions, can we do this with subquestions in multiple choice type questions as well.

www.limesurvey.org/forum/can-i-do-this-w...s-from-a-bucket-of-y

Thank you

Regards
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 1 month ago - 4 years 1 month ago #193760 by Joffm
How many of how many?

I ask this because I have two solutions for that.
The straightforward one and a javascript solution.

Joffm

Here the javascript example for 10 out of 50.
Code:
<script type="text/javascript" charset="utf-8">
 
function shuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex;
 
  // While there remain elements to shuffle...
  while (0 !== currentIndex) {
 
    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;
 
    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }
 
  return array;
}
 
 
  $(document).on('ready pjax:scriptcomplete',function(){
// Fill the array
      var arr = [];
      for (var i = 1; i < 51; i++) {
         arr.push(i);
      }
 
      arr = shuffle(arr);
      anumbers = arr.slice(0,10).join(',');
      $('#question{QID} input[type="text"]').val("#,"+anumbers+",");
    $('#question{QID}').hide();
   });
</script>

You see the places (10, 51) where you have to adapt it.

File Attachment:

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





Joffm

By the way, the example also shows this for a multiple numeric input question.

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 4 years 1 month ago by Joffm.
The following user(s) said Thank You: etc4xx
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 1 month ago - 4 years 1 month ago #193765 by tpartner
Another JavaScript solution...

- Place a multiple-choice question with the same sub-questions before your question.
- Randomize the sub-questions in the new question.
- Hide the new question with a CSS class "hidden".
- Place an array filter on your question based on the new question.
- Insert the script below in the new question. It will check the first "numberToShow" of sub-questions in the hidden question causing the corresponding sub-qustions to be shown in your question.

Code:
<script type="text/javascript" charset="utf-8">
 
  var numberToShow = 5;
 
  $(document).on('ready', function(){    
    if($('#question{QID} input:checkbox:checked').length == 0) {
      $('#question{QID} input:checkbox:lt('+numberToShow+')').each(function(i) {
        $(this).nextAll('input:hidden').val('Y');
        $(this).prop('checked', true).trigger('change');
      }); 
    }
  });
</script>

Sample survey attached:

File Attachment:

File Name: limesurvey...2-10.lss
File Size:37 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 4 years 1 month ago by tpartner.
The following user(s) said Thank You: Joffm, etc4xx
The topic has been locked.
  • etc4xx
  • etc4xx's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
4 years 1 month ago #193790 by etc4xx
Hi Joffm

Thank you for the explanation.

The sample size is in smaller scale, i.e. 4(x) out of 5(y).

Honestly, i am not good using the javascript, may i know the simpler version of doing this?

Regards
The topic has been locked.
  • etc4xx
  • etc4xx's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
4 years 1 month ago #193791 by etc4xx
Hi Tony

Thank you for the explanation.

Will try to check on how to use the script as i am not familiar with it.

Regards
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 1 month ago - 4 years 1 month ago #193806 by Joffm

The sample size is in smaller scale, i.e. 4(x) out of 5(y).

Hi, therefore I asked.

Here it is not necessary to use javascript.

You see, there are only 5 ways to select 4 out of 5.
1,2,3,4
1,2,3,5
1,2,4,5
1,3,4,5
2,3,4,5

So it is sufficient to create a random number between 1 and 5 and set the subquestion relevance accordingly

Subquestion 1: (random!=5)
Subquestion 2: (random!=4)
Subquestion 3: (random!=3)
Subquestion 4: (random!=2)
Subquestion 5: (random!=1)

Joffm

Just calculate "y over x" and decide what is easier for you.

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 4 years 1 month ago by Joffm.
The following user(s) said Thank You: etc4xx
The topic has been locked.
  • etc4xx
  • etc4xx's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
4 years 1 month ago #193809 by etc4xx
Hi Joffm

i have been trying with the script. may i know what are the connecting points between the script and the questions? (i.e. to link the choice of subquestions to the question). Can you show which part of the script i should alter? (if it is possible)



If i intend to do a 10 quiz questions, which consist of single choice questions, multiple choice questions, do i:
a) have to have 1 main script and link it to other question (assuming it shares the same set of answer)
b) have several scripts to cater each of the question. (different answers)

I am interested in the script method, but im having almost no knowledge with regards to it.


Secondly, with regards to the simple method, if the choice of answers is consisting non-numerical option (i.e. yellow , green, red, blue & purple)



Thank you for your time and it is much appreciated.

Regards,
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 1 month ago #193826 by Joffm
Hi,
if you talk about the script I provided:
You see there is one line
Code:
for (var i = 1; i < 51; i++) {
This is to create an array with total numbers from 1 to 50. If you have less or more, adapt.

There is another line
Code:
anumbers = arr.slice(0,10).join(',');
Here you set the numbers of subquestions you want to show.

Then you get a string in the hidden question Q0 like #,10,49,23,1,32,...
Therefore you may comment "//" the line
Code:
$('#question{QID}').hide();
to make this question visible.

And in your question there is a subquestion relevance like strpos(Q0,",1,")>0
Meaning: If the string ",1," is in the calculated random string, the subquestion is displayed.
Read about "strpos" in the manual.

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose