Hi all,
My survey uses a different person's name for each question, and I want them randomized from a list
For example, for Q1 I would pipe in "name1", for Q2 I would pipe in "name2"... so I want to create variables name1 to name30.
I put the following code in the js of an introductory page of my survey that doesn't use a name. I create an array, shuffle the order, then assign values to the new field names.
But even after this page, the names don't show up. Can someone tell me if/where I've done something wrong?
Qualtrics.SurveyEngine.addOnload(function()
{
let names = ["Jennifer", "Jessica", "Amanda", "Ashley", "Sarah", "Stephanie", "Melissa", "Nicole", "Elizabeth",
"Heather", "Tiffany", "Michelle", "Amber", "Megan", "Amy", "Rachel", "Kimberly", "Christina",
"Lauren", "Crystal", "Michael", "Chris", "Matt", "Josh", "David", "James", "Daniel",
"Robert", "John", "Joe", "Jason", "Justin", "Andrew", "Ryan", "William", "Brian", "Brandon", "Jon",
"Nick", "Tony", "Eric", "Adam", "Kevin", "Tom", "Steve", "Tim", "Richard", "Jeremy", "Jeff", "Kyle"];
shuffle(names);
for (let i = 0; i < names.length; i++) {
Qualtrics.SurveyEngine.addEmbeddedData("name"+i, names[i]);
}
});
addEmbeddedData not working
Best answer by SurajK
Hi,
Did you created shuffle function to randomize the array, the above code will give you an error that shuffle is not defined.
Add the below code in JS, it should work fine.
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
Qualtrics.SurveyEngine.addOnload(function()
{
let names = ["Jennifer", "Jessica", "Amanda", "Ashley", "Sarah", "Stephanie", "Melissa", "Nicole", "Elizabeth",
"Heather", "Tiffany", "Michelle", "Amber", "Megan", "Amy", "Rachel", "Kimberly", "Christina",
"Lauren", "Crystal", "Michael", "Chris", "Matt", "Josh", "David", "James", "Daniel",
"Robert", "John", "Joe", "Jason", "Justin", "Andrew", "Ryan", "William", "Brian", "Brandon", "Jon",
"Nick", "Tony", "Eric", "Adam", "Kevin", "Tom", "Steve", "Tim", "Richard", "Jeremy", "Jeff", "Kyle"];
shuffle(names);
for (let i = 0; i < names.length; i++) {
Qualtrics.SurveyEngine.addEmbeddedData("name"+i, names[i]);
}
});
Sign up
Already have an account? Login
Welcome! To join the Qualtrics Experience Community, log in with your existing Qualtrics credentials below.
Confirm your username, share a bit about yourself, Once your account has been approved by our admins then you're ready to explore and connect .
Free trial account? No problem. Log in with your trial credentials to join.
No free trial account? No problem! Register here
Already a member? Hi and welcome back! We're glad you're here 🙂
You will see the Qualtrics login page briefly before being taken to the Experience Community
Login with Qualtrics
Welcome! To join the Qualtrics Experience Community, log in with your existing Qualtrics credentials below.
Confirm your username, share a bit about yourself, Once your account has been approved by our admins then you're ready to explore and connect .
Free trial account? No problem. Log in with your trial credentials to join. No free trial account? No problem! Register here
Already a member? Hi and welcome back! We're glad you're here 🙂
You will see the Qualtrics login page briefly before being taken to the Experience Community
Login to the Community
Welcome! To join the Qualtrics Experience Community, log in with your existing Qualtrics credentials below.
Confirm your username, share a bit about yourself, Once your account has been approved by our admins then you're ready to explore and connect .
Free trial account? No problem. Log in with your trial credentials to join.
No free trial account? No problem! Register here
Already a member? Hi and welcome back! We're glad you're here 🙂
You will see the Qualtrics login page briefly before being taken to the Experience Community
Login with Qualtrics
Welcome! To join the Qualtrics Experience Community, log in with your existing Qualtrics credentials below.
Confirm your username, share a bit about yourself, Once your account has been approved by our admins then you're ready to explore and connect .
Free trial account? No problem. Log in with your trial credentials to join. No free trial account? No problem! Register here
Already a member? Hi and welcome back! We're glad you're here 🙂
You will see the Qualtrics login page briefly before being taken to the Experience Community
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.
