Hello,
I created a number array as follows:
Qualtrics.SurveyEngine.addOnReady(function()
{
var i1=parseInt("${q://QID1/ChoiceTextEntryValue}");
var i2=parseInt("${q://QID2/ChoiceTextEntryValue}");
var i3=parseInt("${q://QID3/ChoiceTextEntryValue}");
var myArray = [i1, i2, i3];
And I want to randomly select one of these items in the array, so I write:
var randomItem = myArray[Math.floor(Math.random() * myArray.length)];
But the problem is, if one of the three items is null (a respondent skill the question), this does not work.
Could you please tell me how I can remove null item in the array?
Thank you very much.
Page 1 / 1
Hello @jhwang ,
Use the below code:
var i1=parseInt("${q://QID1/ChoiceTextEntryValue}") || -1;
var i2=parseInt("${q://QID2/ChoiceTextEntryValue}") || -1;
var i3=parseInt("${q://QID3/ChoiceTextEntryValue}") || -1;
var myArray = [i1, i2, i3];
myArray= jQuery.grep(myArray, function(value) {
return value != -1;
});
var randomItem = myArray[Math.floor(Math.random() * myArray.length)];
Use the below code:
var i1=parseInt("${q://QID1/ChoiceTextEntryValue}") || -1;
var i2=parseInt("${q://QID2/ChoiceTextEntryValue}") || -1;
var i3=parseInt("${q://QID3/ChoiceTextEntryValue}") || -1;
var myArray = [i1, i2, i3];
myArray= jQuery.grep(myArray, function(value) {
return value != -1;
});
var randomItem = myArray[Math.floor(Math.random() * myArray.length)];
Hello @Shashi,
Thank you very much for your code. I think it works for random selection. But I have one additional issue. After the random selection, I want to match another value based on the selected item. So, my code is like:
var i1=parseInt("${q://QID1/ChoiceTextEntryValue}") || -1;
var i2=parseInt("${q://QID2/ChoiceTextEntryValue}") || -1;
var i3=parseInt("${q://QID3/ChoiceTextEntryValue}") || -1;
var myArray = [i1, i2, i3];
myArray= jQuery.grep(myArray, function(value) {
return value != -1;
});
var randomItem = myArray[Math.floor(Math.random() * myArray.length)];
Qualtrics.SurveyEngine.setEmbeddedData("randomItem", randomItem);
var i4="${q://QID4/ChoiceGroup/SelectedChoices}";
var i5="${q://QID5/ChoiceGroup/SelectedChoices}";
var i6="${q://QID6/ChoiceGroup/SelectedChoices}";
var purpose;
if(randomItem = i1){
purpose = i4;
}
else if(randomItem = i2){
purpose = i5;
}
else{
purpose = i6;
}
Qualtrics.SurveyEngine.setEmbeddedData("purpose", purpose);
Finally, in the survey, I want to say like,
"You selected ${e://Field/randomItem} and your purpose was ${e://Field/purpose}."
But with your code, I can see that random selection works, but the latter matching part does not work.
Could you please give me any suggestions?
Thank you very much.
Thank you very much for your code. I think it works for random selection. But I have one additional issue. After the random selection, I want to match another value based on the selected item. So, my code is like:
var i1=parseInt("${q://QID1/ChoiceTextEntryValue}") || -1;
var i2=parseInt("${q://QID2/ChoiceTextEntryValue}") || -1;
var i3=parseInt("${q://QID3/ChoiceTextEntryValue}") || -1;
var myArray = [i1, i2, i3];
myArray= jQuery.grep(myArray, function(value) {
return value != -1;
});
var randomItem = myArray[Math.floor(Math.random() * myArray.length)];
Qualtrics.SurveyEngine.setEmbeddedData("randomItem", randomItem);
var i4="${q://QID4/ChoiceGroup/SelectedChoices}";
var i5="${q://QID5/ChoiceGroup/SelectedChoices}";
var i6="${q://QID6/ChoiceGroup/SelectedChoices}";
var purpose;
if(randomItem = i1){
purpose = i4;
}
else if(randomItem = i2){
purpose = i5;
}
else{
purpose = i6;
}
Qualtrics.SurveyEngine.setEmbeddedData("purpose", purpose);
Finally, in the survey, I want to say like,
"You selected ${e://Field/randomItem} and your purpose was ${e://Field/purpose}."
But with your code, I can see that random selection works, but the latter matching part does not work.
Could you please give me any suggestions?
Thank you very much.
> @jhwang said:
> Hello @Shashi,
> But with your code, I can see that random selection works, but the latter matching part does not work.
> Could you please give me any suggestions?
> Thank you very much.
The if statement should be
` if(randomItem == i1)`
> Hello @Shashi,
> But with your code, I can see that random selection works, but the latter matching part does not work.
> Could you please give me any suggestions?
> Thank you very much.
The if statement should be
` if(randomItem == i1)`
Thank you, @Shashi.
I tried to change the if statement in the same way you suggested. But finally, I got the result like this:
`if(randomItem == "${q://QID1/ChoiceTextEntryValue}")`
I tried to change the if statement in the same way you suggested. But finally, I got the result like this:
`if(randomItem == "${q://QID1/ChoiceTextEntryValue}")`
Leave a Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.