Welcome to the LimeSurvey Community Forum

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

Randomly assigned numbers with if-statement

  • Snout23
  • Snout23's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
2 years 1 day ago - 2 years 23 hours ago #239924 by Snout23
Please help us help you and fill where relevant:
Your LimeSurvey version: 3.25.11
Own server or LimeSurvey hosting: Own Server
Survey theme/template: /
==================
Hello everybody,
I´m using the following JavaScript out of this forum to generate random numbers and store them in a text question:

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 = ["001",
"002",
"003",
"004",
"005",
"006",
"007",
"008"
];
      arr = shuffle(arr);
      anumbers = arr.slice(0,4).join(','); 
      $('#question{QID} input[type="text"]').val("#"+anumbers);
    $('#question{QID}').hide(); 
   } );
</script>


This works fine but I have to do a small adjustment: If participants have answered a previous closed question with the answer option "A1", there should only be the numbers "002" to "008" in the array from which the random numbers are drawn. Else, there should be the numbers "001" to "006". I think this could be easily down with an if/else-Statement and I´ve already tried by myself, but since I´m not very familiar with JavaScript, I can´t get it working probably. To sum up, the logic would be the following:
if variable1 = "A1" variable arr only contains the numbers "002" to "008", else the variable arr only contains the numbers "001" to "006". 

Here´s how I´ve tried it, but it doesn´t work:

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;
}
 
if (GESCHLECHT.shown=="A1") { 
 
    $(document).on('ready pjax:scriptcomplete',function(){
// Fill the array
      var arr = [
"002",
"003",
"004",
"005",
"006",
"007",
"008"
]} }
 
else {
$(document).on('ready pjax:scriptcomplete',function(){
// Fill the array
      var arr = ["001",
"002",
"003",
"004",
"005",
"006"
]} }
;
      arr = shuffle(arr);
      anumbers = arr.slice(0,4).join(','); 
      $('#question{QID} input[type="text"]').val("#"+anumbers);
//      $('#question{QID}').hide(); 
   } );
</script>
 

Thanks in advance!

 
Last edit: 2 years 23 hours ago by Snout23. Reason: Additional information

Please Log in to join the conversation.

More
2 years 23 hours ago #239925 by jelo

Code:
if (GESCHLECHT.shown=="A1") {
 

Javascript isn't aware of the questioncodes and mixing ExpressionScript and Javascript is limited.
ExpressionScript variables can be placed in the HTML Code. And JavaScript is part of the HTML code.

You define a Javascript variable and assign the value of the ExpressionScript variable (e.g. Questioncodename).
Code:
// Answer via Code
var geschlechtJavaScriptVariable = '{Qgeschlecht.NAOK}';
if (geschlechtJavaScriptVariable == 'A1') { // }  

The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
The following user(s) said Thank You: Snout23

Please Log in to join the conversation.

  • Snout23
  • Snout23's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
2 years 23 hours ago #239926 by Snout23
Replied by Snout23 on topic Randomly assigned numbers with if-statement
Thank you very much :) I´ll try this!

Please Log in to join the conversation.

Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose