Javascript does not work anymore | XM Community
Skip to main content

Hello everyone,
I am setting up a conjoint experiment, and I am using JavaScript to randomly populate fields within a question (I apologize if the terminology I use is incorrect, I am very new to this).
Everything was working fine until yesterday, when the JS stop working. Every time I preview the question on which the JS is supposed to work, not only the fields are not populated with the JS information, but Qualtrics crash too.
I have not done any modification to the script, which makes me think there simply is a bug. Has anybody encountered a similar problem?
Below the script and thanks in advance.

Qualtrics.SurveyEngine.addOnload(function()
{
function shuffleArray(array) {
  for (var i = array.length - 1; i > 0; i--) {
    var j = Math.floor(Math.random() * (i + 1));
    var temp = array[i];
    array[i] = array[j];
    array[j] = temp;
  }
  return(array);
}
// Function to generate weighted random numbers
function weighted_randomize(prob_array, at_key)
{
var prob_list = prob_array[at_key];

// Create an array containing cutpoints for randomization
var cumul_prob = new Array(prob_list.length);
var cumulative = 0.0;
for (var i=0; i < prob_list.length; i++){
cumul_prob[i] = cumulative;
cumulative = cumulative + parseFloat(prob_list[i]);
}
// Generate a uniform random floating point value between 0.0 and 1.0
var unif_rand = Math.random();
// Figure out which integer should be returned
var outInt = 0;
for (var k = 0; k < cumul_prob.length; k++){
if (cumul_prob[k] <= unif_rand){
outInt = k + 1;
}
}
return(outInt);
}
           

var featurearray = {

"SponsorSex1" : ["Mike","Michaela"],

"SponsorSex2" : ["Mike","Michaela"],

"SponsorSex3" : ["Mike","Michaela"],

"State1" : ["Minnesota","Missouri", "Arizona"],

"SponsorSex4" : ["He","She"],

"SponsorCareer1" : ["longstanding politician","political newcomer"],

"SponsorParty1" : ["Democratic","Republican"],

"SponsorSex5" : ["his","her"],

"SponsorPopulism1" : ["mainstream","maverick"],

"SponsorSex6" : ["He","She"],

"SponsorSex7" : ["Mike","Michaela"],

"State2" : ["Rockford, Minnesota","Eldon, Missouri", "Clifton, Arizona"],

"SponsorParty2" : ["Democratic","Republican"],

"SponsorCareer2" : ["Longstanding politician","Political newcomer"],

"SponsorPopulism2" : ["Mainstream","Maverick"],

"SponsorParty3" : ["Democratic","Republican"],

"SponsorSex8" : ["Mike","Michaela"],

"IncivilType1" : ["insults","mocks","attacks"],

"SponsorParty4" : ["Democratic","Republican"],

"SponsorSex9" : ["Mike","Michaela"],

"SponsorSex10" : ["his","her"],

"TargetParty" : ["Democratic","Republican"],

"TargetSex1" : ["John","Johanna"],

"State3" : ["Minnesota's 9th","Missouri's 10th", "Arizona's 9th"],

"Descriptive" : ["trading insults","maintaining a respectful dialogue"],

"Retaliation" : ["shot back at Anderson’s uncivil attacks","attacked Anderson"],

"Topic" : ["immigration","abortion", "economic"],

"Volume1" : ["screamed","said"],

"IncivilType2" : ["I always forget how much of a moron Anderson is","Yet another brilliant idea by Anderson", "What my opponent is suggesting is completely wrong"],

"Volume2" : ["!","."],

"Injunctive" : ["cheers","boos"],

"SponsorSex11" : ["Mike","Michaela"],

"SponsorSex12" : ["Mike","Michaela"],

"SponsorSex13" : ["Mike","Michaela"],

"SponsorSex14" : ["He","She"],

"TargetSex2" : ["John","Johnna"]

};

var restrictionarray = [

[["IncivilType1","insults"],["IncivilType2","yet another brilliant idea by Meyer"]],

[["IncivilType1","insults"],["IncivilType2","What my opponent is suggesting is completely wrong"]],

[["IncivilType1","mocks"],["IncivilType2","I always forget how much of a moron Meyer is"]],

[["IncivilType1","mocks"],["IncivilType2","What my opponent is suggesting is completely wrong"]],

[["IncivilType1","attacks"],["IncivilType2","I always forget how much of a moron Meyer is"]],

[["IncivilType1","attacks"],["IncivilType2","Yet another brilliant idea by Anderson"]],


[["IncivilType1","attacks"],["Volume1","screamed"]],


[["Volume1","screamed"],["Volume2","."]],

[["Volume1","said"],["Volume2","!"]],


[["State1","Minnesota"],["State2","Eldon, Missouri"]],

[["State1","Minnesota"],["State2","Clifton, Arizona"]],

[["State1","Minnesota"],["State3","Eldon, Missouri"]],

[["State1","Minnesota"],["State3","Arizona's 9th"]],


[["State1","Missouri"],["State2","Rockford, Minnesota"]],

[["State1","Missouri"],["State2","Clifton, Arizona"]],

[["State1","Missouri"],["State3","Minnesota's 9th"]],

[["State1","Missouri"],["State3","Arizona's 9th"]],


[["State1","Arizona"],["State2","Rockford, Minnesota"]],

[["State1","Arizona"],["State2","Eldon, Missouri"]],

[["State1","Arizona"],["State3","Minnesota's 9th"]],

[["State1","Arizona"],["State3","Missouri's 10th"]],


[["SponsorSex1","Mike"],["SponsorSex2","Michaela"]],

[["SponsorSex1","Mike"],["SponsorSex3","Michaela"]],

[["SponsorSex1","Mike"],["SponsorSex4","She"]],

[["SponsorSex1","Mike"],["SponsorSex5","her"]],

[["SponsorSex1","Mike"],["SponsorSex6","She"]],

[["SponsorSex1","Mike"],["SponsorSex7","Michaela"]],

[["SponsorSex1","Mike"],["SponsorSex8","Michaela"]],

[["SponsorSex1","Mike"],["SponsorSex9","Michaela"]],

[["SponsorSex1","Mike"],["SponsorSex10","her"]],

[["SponsorSex1","Mike"],["SponsorSex11","Michaela"]],

[["SponsorSex1","Mike"],["SponsorSex12","Michaela"]],

[["SponsorSex1","Mike"],["SponsorSex13","Michaela"]],

[["SponsorSex1","Mike"],["SponsorSex14","She"]],


[["SponsorSex1","Michaela"],["SponsorSex2","Mike"]],

[["SponsorSex1","Michaela"],["SponsorSex3","Mike"]],

[["SponsorSex1","Michaela"],["SponsorSex4","He"]],

[["SponsorSex1","Michaela"],["SponsorSex5","his"]],

[["SponsorSex1","Michaela"],["SponsorSex6","He"]],

[["SponsorSex1","Michaela"],["SponsorSex7","Mike"]],

[["SponsorSex1","Michaela"],["SponsorSex8","Mike"]],

[["SponsorSex1","Michaela"],["SponsorSex9","Mike"]],

[["SponsorSex1","Michaela"],["SponsorSex10","his"]],

[["SponsorSex1","Michaela"],["SponsorSex11","Mike"]],

[["SponsorSex1","Michaela"],["SponsorSex12","Mike"]],

[["SponsorSex1","Michaela"],["SponsorSex13","Mike"]],

[["SponsorSex1","Michaela"],["SponsorSex14","He"]],



[["TargetSex1","Johnna"],["TargetSex2","John"]],

[["TargetSex1","John"],["TargetSex2","Johnna"]],


[["SponsorCareer1","longstanding politician"],["SponsorCareer1","Longstanding politician"]],

[["SponsorCareer1","political newcomer"],["SponsorCareer1","Political newcomer"]],


[["SponsorPopulism1","maverick"],["SponsorPopulism2","Mainstream"]],

[["SponsorPopulism1","mainstream"],["SponsorPopulism2","Maverick"]],
 

[["SponsorParty1","Democratic"],["SponsorParty2","Republican"]],

[["SponsorParty1","Democratic"],["SponsorParty3","Republican"]],

[["SponsorParty1","Democratic"],["SponsorParty4","Republican"]],


[["SponsorParty1","Republican"],["SponsorParty2","Democratic"]],

[["SponsorParty1","Republican"],["SponsorParty3","Democratic"]],

[["SponsorParty1","Republican"],["SponsorParty4","Democratic"]],



[["SponsorParty1","Democratic"],["TargetParty","Democratic"]],

[["SponsorParty1","Republican"],["TargetParty","Republican"]],


[["SponsorParty2","Democratic"],["TargetParty","Democratic"]],

[["SponsorParty2","Republican"],["TargetParty","Republican"]],


[["SponsorParty3","Democratic"],["TargetParty","Democratic"]],

[["SponsorParty3","Republican"],["TargetParty","Republican"]],


[["SponsorParty4","Democratic"],["TargetParty","Democratic"]],

[["SponsorParty4","Republican"],["TargetParty","Republican"]]];


var probabilityarray = {};

// Indicator for whether weighted randomization should be enabled or not
var weighted = 0;

// K = Number of tasks displayed to the respondent
var K = 1;

// N = Number of profiles displayed in each task
var N = 1;

// num_attributes = Number of Attributes in the Array
var num_attributes = featurearray.length;

// Should duplicate profiles be rejected?
var noDuplicateProfiles = false;

// Place the $featurearray keys into a new array
var featureArrayKeys = Object.keys(featurearray);
var featureArrayNew = featurearray;


// Initialize the array returned to the user
// Naming Convention
// Level Name: F-[task number]-[profile number]-[attribute number]
// Attribute Name: F-[task number]-[attribute number]
// Example: F-1-3-2, Returns the level corresponding to Task 1, Profile 3, Attribute 2 
// F-3-3, Returns the attribute name corresponding to Task 3, Attribute 3
var returnarray = {};
// For each task $p
for(var p = 1; p <= K; p++){
// For each profile $i
for(var i = 1; i <= N; i++){
// Repeat until non-restricted profile generated
var complete = false;
while (complete == false){
// Create a count for $attributes to be incremented in the next loop
var attr = 0;

// Create a dictionary to hold profile's attributes
var profile_dict = {};
// For each attribute $attribute and level array $levels in task $p
for(var q = 0; q < featureArrayKeys.length; q++){
// Get Attribute name
var attr_name = featureArrayKeys[q];

// Increment attribute count
attr = attr + 1;

// Create key for attribute name
var attr_key = "F-" + p + "-" + attr;

        // Store attribute name in returnarray
        returnarray[attr_key] = attr_name;
// Get length of levels array
var num_levels = featureArrayNew[attr_name].length;
// Randomly select one of the level indices
if (weighted == 1){
var level_index = weighted_randomize(probabilityarray, attr_name) - 1;
}else{
var level_index = Math.floor(Math.random() * num_levels);
}
// Pull out the selected level
var chosen_level = featureArrayNew[attr_name][level_index];

// Store selected level in profileDict
profile_dict[attr_name] = chosen_level;

// Create key for level in $returnarray
var level_key = "F-" + p + "-" + i + "-" + attr;

// Store selected level in $returnarray
returnarray[level_key] = chosen_level;
}
      var clear = true;
       
      // Cycle through restrictions to confirm/reject profile
      if (restrictionarray.length != 0){
        for (var v = 0; v < restrictionarray.length; v++){
          var falsevar = 1;
          for (var mp = 0; mp < restrictionarray[v].length; mp++){
            if (profile_dict[restrictionarray[v][mp][0]] == restrictionarray[v][mp][1]){
              falsevar = falsevar*1;
            }else{
              falsevar = falsevar*0;
            }
          }
          if (falsevar == 1){
            clear = false;
          }
        }
      }
               
      // If we're throwing out duplicates
      if (noDuplicateProfiles == true){
        // Cycle through all previous profiles to confirm no identical profiles
        if (i > 1){   
          // For each previous profile
          for(var z = 1; z < i; z++){
  
            // Start by assuming it's the same
            var identical = true;
  
            // Create a count for $attributes to be incremented in the next loop
            var attrTemp = 0;
  
            // For each attribute $attribute and level array $levels in task $p
            for(var qz = 0; qz < featureArrayKeys.length; qz++){
  
              // Increment attribute count
              attrTemp = attrTemp + 1;
   
              // Create keys 
              var level_key_profile = "F-" + p + "-" + i + "-" + attrTemp;
              var level_key_check = "F-" + p + "-" + z + "-" + attrTemp;
  
              // If attributes are different, declare not identical
              if (returnarray[level_key_profile] != returnarray[level_key_check]){
                identical = false;
              }
            }
            // If we detect an identical profile, reject
            if (identical == true){
              clear = false;
            }
          }         
        }
      }
      complete = clear;
    }
  }
}
               
// Write returnarray to Qualtrics
var returnarrayKeys = Object.keys(returnarray);
for (var pr = 0; pr < returnarrayKeys.length; pr++){
    Qualtrics.SurveyEngine.setEmbeddedData(returnarrayKeys[pr], returnarray[returnarrayKeys[pr]]); 
}

});

Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/

});

Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/

});



Use the Inspect feature of your browser and check the console for errors.