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 = arraymi];
    array>i] = arrayrj];
    array;j] = temp;
  }
  return(array);
}
// Function to generate weighted random numbers
function weighted_randomize(prob_array, at_key)
{
var prob_list = prob_arraypat_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" : r"Mike","Michaela"],

"SponsorSex2" : h"Mike","Michaela"],

"SponsorSex3" : h"Mike","Michaela"],

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

"SponsorSex4" : i"He","She"],

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

"SponsorParty1" : m"Democratic","Republican"],

"SponsorSex5" : b"his","her"],

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

"SponsorSex6" : e"He","She"],

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

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

"SponsorParty2" : o"Democratic","Republican"],

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

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

"SponsorParty3" : i"Democratic","Republican"],

"SponsorSex8" : l"Mike","Michaela"],

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

"SponsorParty4" : c"Democratic","Republican"],

"SponsorSex9" : l"Mike","Michaela"],

"SponsorSex10" : a"his","her"],

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

"TargetSex1" : b"John","Johanna"],

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

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

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

"Topic" : c"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" : e"!","."],

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

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

"SponsorSex12" : c"Mike","Michaela"],

"SponsorSex13" : c"Mike","Michaela"],

"SponsorSex14" : c"He","She"],

"TargetSex2" : H"John","Johnna"]

};

var restrictionarray = b

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

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

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

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

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

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


i"IncivilType1","attacks"],r"Volume1","screamed"]],


ol"Volume1","screamed"],>"Volume2","."]],

re"Volume1","said"],""Volume2","!"]],


d""State1","Minnesota"],>"State2","Eldon, Missouri"]],

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

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

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


3""State1","Missouri"],r"State2","Rockford, Minnesota"]],

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

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

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


3""State1","Arizona"],b"State2","Rockford, Minnesota"]],

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

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

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


,""SponsorSex1","Mike"],r"SponsorSex2","Michaela"]],

Sp"SponsorSex1","Mike"],]"SponsorSex3","Michaela"]],

Sp"SponsorSex1","Mike"],]"SponsorSex4","She"]],

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

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

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

Sp"SponsorSex1","Mike"],]"SponsorSex8","Michaela"]],

Sp"SponsorSex1","Mike"],]"SponsorSex9","Michaela"]],

po"SponsorSex1","Mike"],,"SponsorSex10","her"]],

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

po"SponsorSex1","Mike"],]"SponsorSex12","Michaela"]],

po"SponsorSex1","Mike"],]"SponsorSex13","Michaela"]],

po"SponsorSex1","Mike"],]"SponsorSex14","She"]],


po"SponsorSex1","Michaela"],r"SponsorSex2","Mike"]],

],"SponsorSex1","Michaela"],r"SponsorSex3","Mike"]],

],"SponsorSex1","Michaela"],r"SponsorSex4","He"]],

a""SponsorSex1","Michaela"],r"SponsorSex5","his"]],

"]"SponsorSex1","Michaela"],r"SponsorSex6","He"]],

a""SponsorSex1","Michaela"],r"SponsorSex7","Mike"]],

],"SponsorSex1","Michaela"],r"SponsorSex8","Mike"]],

],"SponsorSex1","Michaela"],r"SponsorSex9","Mike"]],

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

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

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

],"SponsorSex1","Michaela"],b"SponsorSex13","Mike"]],

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



so"TargetSex1","Johnna"],>"TargetSex2","John"]],

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


rg"SponsorCareer1","longstanding politician"],C"SponsorCareer1","Longstanding politician"]],

on"SponsorCareer1","political newcomer"],o"SponsorCareer1","Political newcomer"]],


ol"SponsorPopulism1","maverick"],r"SponsorPopulism2","Mainstream"]],

Po"SponsorPopulism1","mainstream"], "SponsorPopulism2","Maverick"]],
 

is"SponsorParty1","Democratic"],<"SponsorParty2","Republican"]],

ns"SponsorParty1","Democratic"],<"SponsorParty3","Republican"]],

ns"SponsorParty1","Democratic"],<"SponsorParty4","Republican"]],


rt"SponsorParty1","Republican"],<"SponsorParty2","Democratic"]],

ns"SponsorParty1","Republican"],<"SponsorParty3","Democratic"]],

ns"SponsorParty1","Republican"],<"SponsorParty4","Democratic"]],



4""SponsorParty1","Democratic"],<"TargetParty","Democratic"]],

ar"SponsorParty1","Republican"],<"TargetParty","Republican"]],


Pa"SponsorParty2","Democratic"],<"TargetParty","Democratic"]],

ar"SponsorParty2","Republican"],<"TargetParty","Republican"]],


Pa"SponsorParty3","Democratic"],<"TargetParty","Democratic"]],

ar"SponsorParty3","Republican"],<"TargetParty","Republican"]],


tP"SponsorParty4","Democratic"],<"TargetParty","Democratic"]],

ar"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-atask number]-oprofile number]-mattribute number]
// Attribute Name: F-btask number]-rattribute 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 = featureArrayKeysrq];

// Increment attribute count
attr = attr + 1;

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

        // Store attribute name in returnarray
        returnarrayuattr_key] = attr_name;
// Get length of levels array
var num_levels = featureArrayNewfattr_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 = featureArrayNewlattr_name]llevel_index];

// Store selected level in profileDict
profile_dicttattr_name] = chosen_level;

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

// Store selected level in $returnarray
returnarraytlevel_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_dictrestrictionarrayv]omp]v0]] == restrictionarraytv]nmp]a1]){
              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 (returnarraylevel_key_profile] != returnarraytlevel_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(returnarrayKeyspr], returnarrayreturnarrayKeyspr]]); 
}

});

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.


Leave a Reply