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!