Welcome to the LimeSurvey Community Forum

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

Randomize and subset questions from an array (10 point choice)

  • afu4500
  • afu4500's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 8 months ago #203842 by afu4500
Hello everybody
i'm a complete newbie to limesurvey.
is it possible to display 5 questions ranodmized from an array of 10 questions? how is this done?

Thanks for your help
Attachments:
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 8 months ago #203847 by Joffm
Hi,
first you have to generate randomly 5 out of 10 numbers.
Therefoe you create a question of type "short text" and enter the following javascript snippet in source code mode.
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 =["A","B","C","D","E","F","G","H","I","J"];
      arr = shuffle(arr);
      anumbers = '#' + arr.slice(0,5).join(',');
      $('#question{QID} input[type="text"]').val(anumbers).trigger('keyup');  
      $('#question{QID}').hide();
   });
</script>

You see there is initially an array with te letters "A-J".
This array is shuffled and in the next line only the first 5 elements are left.
This array is joined to a string and entered into the question
and the question is hidden.

And in your array it is only necessary to check if the string contains a certain letter.
Subquestion 1: strpos(Q1,"A">0
Subquestion 2: strpos(Q1,"B">0



File Attachment:

File Name: limesurvey...3378.lss
File Size:27 KB


Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
  • afu4500
  • afu4500's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 8 months ago #203869 by afu4500
Hi Joffm

Thanks for your quick response. I have inserted the code in a short text question.
Unfortunately, when I go to the survey preview, I don't see an array. Does the code create a new array or does it refer to an already created one? If so, do I need to add the ID of the created matrix to your code?

Thanks for your help
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 8 months ago - 3 years 8 months ago #203872 by Joffm
Hi,
which array are you talking of?

The temporary created array in the javascript snippet?
You never will see this.

If you comment this line
// $('#question{QID}').hide();

you should see in the short text something like this


Or do you talk about YOUR 10-point-array?
Of course you have to create this an use the subquestion relevance I showed.

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 3 years 8 months ago by Joffm.
The topic has been locked.
  • afu4500
  • afu4500's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 8 months ago #203874 by afu4500
Hi Joffm,

Sorry for my incomprehensible expression.
The short text question with the code shows me randomly 5 letters. This works.
But the array I created still shows all the sub-questions.
Do I also have to insert the code in the source code of the array or what does the code in the source code of the short text question do for me?

Thanks for your patience
The topic has been locked.
  • afu4500
  • afu4500's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 8 months ago #203877 by afu4500
Sorry, I didn't see your last sentence about subquestion relevance.
I adapted the subquestion relevance as seen in the photo.
There seems to be something wrong when I check the logic. What should I change?

Thanks for your help.
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 8 months ago #203881 by Joffm
Sorry, my fault
The closing bracket doesn't work well on my keyboard.

strpos(Q1,"A")>0

as it is explained in the manual about "implemented functions".

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
  • afu4500
  • afu4500's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 8 months ago #203883 by afu4500
Awsome thank you so much, it worked.

Do you have an idea how to add 2 fields at the end of the array,
so that the participants can write something themselves and rate it from 1-10.

Thx
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 8 months ago - 3 years 8 months ago #203886 by Joffm
Yes
forums.limesurvey.org/forum/design-issue...ith-textfield-others

You can extend to two, three, four,... others

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 3 years 8 months ago by Joffm.
The topic has been locked.
  • afu4500
  • afu4500's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 8 months ago #203887 by afu4500
I really appreciate your help.
Unfortunately I cannot download the attached file because the network of the company I work for does not allow it.
Can you send me the code or post it in the forum as before.

Thx
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose