I’m trying to do some calculations in the addOnLoad function, and then assign the result to embedded data variables which were defined in Survey Flow at the top of the survey. When I look at the calculations (printing to console.log and then viewing with Inspect) they are correct. But for some reason the assignment to embedded data variables seems to be going wrong.
In the Javascript code I have an array with 7 members, all initialized to zero, and for two randomly selected members I flip the value from 0 to 1.
There are 7 embedded data variables, and at the end of my Javascript code each member of the array is assigned to one of these. When I look at the final array using Inspect, it’s fine. But then the embedded data variables are wrong. In each case 2 of the 7 are indeed set to 1, but it’s rarely the correct 2. But sometimes it is correct! Maybe random chance??
Here’s my code:
Qualtrics.SurveyEngine.addOnload(function()
{
/* this function will randomly select two purchasing decision blocks for display */
/* depending on the answers to previous questions H11 and WH3 */
/* first determine if respondent has central AC (H11) or an electric water heater (WH3) */
/*var HasCentralAC = "${e://Field/HasCentralAC}";
var HasElectricWaterHeater = "${e://Field/HasElectricWaterHeater}";*/
var HasCentralAC = 1;
var HasElectricWaterHeater = 1;
console.log("HasCentralAC: ");
console.log(HasCentralAC); /* 1 = has central AC */
console.log("HasElectricWaterHeater: ");
console.log(HasElectricWaterHeater); /* 1 = has electric water heater */
/* initialize array of blocks */
if (HasCentralAC == 1 && HasElectricWaterHeater == 1)
var PDBlocks = [1, 2, 3, 4, 5, 6, 7]; /* includes all blocks */
else if (HasCentralAC == 1 && HasElectricWaterHeater != 1)
var PDBlocks = [1, 2, 3, 4, 6, 7]; /* excludes water heater block */
else if (HasCentralAC != 1 && HasElectricWaterHeater == 1)
var PDBlocks = [1, 3, 4, 5, 6, 7]; /* excludes central AC block */
else if (HasCentralAC != 1 && HasElectricWaterHeater != 1)
var PDBlocks = [1, 3, 4, 6, 7]; /* excludes both */
console.log("PDBlocks: ");
console.log(PDBlocks);
/* what is the maximum number of blocks in the array? */
var GroupPop = PDBlocks.length;
console.log("GroupPop: ");
console.log(GroupPop);
/* randomly select two elements of the array, but make sure they are different from each other */
do {
var ChosenArrayIndex1 = Math.floor(Math.random() * GroupPop);
var ChosenArrayIndex2 = Math.floor(Math.random() * GroupPop);
}
while (ChosenArrayIndex1==ChosenArrayIndex2);
console.log("Randomly selected array indexes (remember index starts with 0): ");
console.log(ChosenArrayIndex1);
console.log(ChosenArrayIndex2);
console.log("Which blocks were randomly chosen?");
console.log(PDBlocks[ChosenArrayIndex1]);
console.log(PDBlocks[ChosenArrayIndex2]);
/* set flags for the selected blocks */
/* first set all flags to 0 (false) for all 7 blocks */
/* then set flag to 1 (true) for the two randomly selected blocks */
var ShowBlockFlags = [0, 0, 0, 0, 0, 0, 0]
ShowBlockFlags[PDBlocks[ChosenArrayIndex1]-1] = 1;
ShowBlockFlags[PDBlocks[ChosenArrayIndex2]-1] = 1;
console.log("ShowBlockFlags: ");
console.log(ShowBlockFlags);
/* now set embedded data variables according to flags */
Qualtrics.SurveyEngine.setEmbeddedData('ShowBlock1', ShowBlockFlags[0]);
Qualtrics.SurveyEngine.setEmbeddedData('ShowBlock2', ShowBlockFlags[1]);
Qualtrics.SurveyEngine.setEmbeddedData('ShowBlock3', ShowBlockFlags[2]);
Qualtrics.SurveyEngine.setEmbeddedData('ShowBlock4', ShowBlockFlags[3]);
Qualtrics.SurveyEngine.setEmbeddedData('ShowBlock5', ShowBlockFlags[4]);
Qualtrics.SurveyEngine.setEmbeddedData('ShowBlock6', ShowBlockFlags[5]);
Qualtrics.SurveyEngine.setEmbeddedData('ShowBlock7', ShowBlockFlags[6]);
});
