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

Piping in random subset of statements from matrix question to another matrix question

  • January 18, 2022
  • 2 replies
  • 46 views

Forum|alt.badge.img+2

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

2 replies

Rudi
QPN Level 3 ●●●
Forum|alt.badge.img+16
  • QPN Level 3 ●●●
  • January 19, 2022

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] = arr[x in taken ? taken[x] : x];
    taken[x] = --len in taken ? taken[len] : len;
  }
  console.log(result)
return result;
}
Qualtrics.SurveyEngine.addOnPageSubmit(function()
{
let qid = this.questionId
let qInfo = Qualtrics.SurveyEngine.QuestionInfo
let choices = qInfo[qid].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[keys].RecodeValue)
}
for(let i=0;i selectedAnswers.push(Number(this.getSelectedAnswerValue(availableBrands[i])))
}
for(let i=0;i if(selectedAnswers[i] > 3 ){
eligibleBrands.push(availableBrands[i])
}
}
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(eligibleBrands[i]!=1){
tempEligibleBrands.push(eligibleBrands[i])
}
}
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


Forum|alt.badge.img+2
  • Author
  • January 19, 2022

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.