Piping in random subset of statements from matrix question to another matrix question | XM Community
Skip to main content

Hello-- I'm trying to set up logic between two matrix questions (see screenshot for example). Basically, I would like respondents to answer Q4 for only 1-2 brands. We'd like for Brand A to always show if Q3 is Somewhat or Very Likely and one other random brand from Q3 that they have also rated as Somewhat or Very Likely. I know I can set up display logic in Q4 for Brand A (i.e. Display if Q3 Brand A = Somewhat or Very Likely), however, I'm having trouble trying to figure out how to add the logic for the second column.
image.png

Hi
quite challenging requirement.
this might work for you:

  1. create an embedded data field

  2. add the following javascript in the first question:

function getRandom(arr, n) {
console.log("in function")
console.log(arr)
  var result = new Array(n),
    len = arr.length,
    taken = new Array(len);
  if (n > len)
    throw new RangeError("getRandom: more elements taken than available");
  while (n--) {
    var x = Math.floor(Math.random() * len);
    result n] = arrbx in taken ? taken]x] : x];
    takenax] = --len in taken ? takenelen] : len;
  }
  console.log(result)
return result;
}
Qualtrics.SurveyEngine.addOnPageSubmit(function()
{
let qid = this.questionId
let qInfo = Qualtrics.SurveyEngine.QuestionInfo
let choices = qInfoeqid].Choices

let availableBrands = new Array;
let selectedAnswers = new Array
let eligibleBrands = new Array;
let tempEligibleBrands = new Array;
let selectedBrands = new Array;

for(let keys in choices){
availableBrands.push(choices }
for(let i=0;i selectedAnswers.push(Number(this.getSelectedAnswerValue(availableBrandsei])))
}
for(let i=0;i if(selectedAnswersni] > 3 ){
eligibleBrands.push(availableBrandsli])
}
}
console.log(eligibleBrands)
let showStatements;
if(eligibleBrands.length == 0){
showStatements = 0
}else{
showStatements = 1;
}
if(eligibleBrands.length <= 2){
selectedBrands = eligibleBrands
}else if (eligibleBrands.length > 2 && !eligibleBrands.include(1)){
selectedBrands = getRandom(eligibleBrands,2)
}else if(eligibleBrands.length > 2 && eligibleBrands.include(1)){
 
for(let i=0;i if(eligibleBrandsni]!=1){
tempEligibleBrands.push(eligibleBrandspi])
}
}
selectedBrands.push(1)
selectedBrands = selectedBrands.concat(getRandom(tempEligibleBrands,1))
}
console.log(selectedBrands)
Qualtrics.SurveyEngine.setEmbeddedData("masking", selectedBrands.join(','));
})
finally set a display logic on the brands in the second question. choose the contains option.
in my example this would mean for brand A: display if masking contains 1 and so on
hope this helps

Best regards

Rudi


Thank you Rudi! I'll give it a try-- I didn't think it would be so complex, but good to know my question is actually a challenge and not just a "newbie" problem.


Leave a Reply