Randomly selecting one of four word then displaying it on four questions without selecting a word twice! | XM Community
Skip to main content
Solved

Randomly selecting one of four word then displaying it on four questions without selecting a word twice!


Forum|alt.badge.img+1

Hi,

I have four words (cat, car, mountain, ocean).

And I have four different questions (but past and future conditions) 

1- recall a past event with word ….

2- recall a past event with word ….

3- imagine a future event with word ….

4- imagine a future event with word ….

 

I want to randomly and equally assign these words to these conditions for each participant but I do not want to get ‘cat’ to be displayed twice for instance. 

How can I do that? Should I embedded data? I appreciate your help!!

Best answer by Nam Nguyen

@FritzBreithaupt 

Add this Java code in a Text/Graphic question at the begining of your survey.

Qualtrics.SurveyEngine.addOnload(function() {
    var words = ['cat', 'car', 'mountain', 'ocean'];

    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;
        }
    }
    shuffleArray(words);

    Qualtrics.SurveyEngine.setEmbeddedData('PastEventWord1', words[0]);
    Qualtrics.SurveyEngine.setEmbeddedData('PastEventWord2', words[1]);
    Qualtrics.SurveyEngine.setEmbeddedData('FutureEventWord1', words[2]);
    Qualtrics.SurveyEngine.setEmbeddedData('FutureEventWord2', words[3]);
});

This will give you 4 embedded data with 4 word and no duplication

Then display the embedded data to you recall/imagine question with pip-text (for example 1- recall a past event with word ${e://Field/PastEventWord1})

Note: Add page brake between the Text/Graphic question with Javascript and the Recall/Imagine question or put them on different block. Let the embedded data space to get set value.
Let me know if this helps

View original

3 replies

Nam Nguyen
QPN Level 8 ●●●●●●●●
Forum|alt.badge.img+28
  • QPN Level 8 ●●●●●●●●
  • 1090 replies
  • Answer
  • August 31, 2023

@FritzBreithaupt 

Add this Java code in a Text/Graphic question at the begining of your survey.

Qualtrics.SurveyEngine.addOnload(function() {
    var words = ['cat', 'car', 'mountain', 'ocean'];

    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;
        }
    }
    shuffleArray(words);

    Qualtrics.SurveyEngine.setEmbeddedData('PastEventWord1', words[0]);
    Qualtrics.SurveyEngine.setEmbeddedData('PastEventWord2', words[1]);
    Qualtrics.SurveyEngine.setEmbeddedData('FutureEventWord1', words[2]);
    Qualtrics.SurveyEngine.setEmbeddedData('FutureEventWord2', words[3]);
});

This will give you 4 embedded data with 4 word and no duplication

Then display the embedded data to you recall/imagine question with pip-text (for example 1- recall a past event with word ${e://Field/PastEventWord1})

Note: Add page brake between the Text/Graphic question with Javascript and the Recall/Imagine question or put them on different block. Let the embedded data space to get set value.
Let me know if this helps


Forum|alt.badge.img+1

Wow, it works amazing thank you so much for explaining so clearly!


Nam Nguyen
QPN Level 8 ●●●●●●●●
Forum|alt.badge.img+28
  • QPN Level 8 ●●●●●●●●
  • 1090 replies
  • September 2, 2023

@FritzBreithaupt Happy to helps 🙏


Leave a Reply