Qualtrics结合JavaScript实现句子随机插入不重复词汇的技术求助
Hey there! Let's work through your Qualtrics requirement—showing one sentence per page, inserting unique random words into each, while keeping the sentence order consistent across all respondents. Here's a step-by-step solution:
First, Let's Break Down Your Core Needs
- Fixed sentence order for all participants
- Each sentence gets a unique random word (no repeats across pages for a single respondent)
- Words are pulled from a predefined pool, with no reuse once assigned
What's Wrong with the Existing Code?
The code you shared does shuffle the word list and store them as embedded data (name0, name1, etc.), but it doesn't connect these stored words to your individual sentence pages. You need to link each embedded data field to the corresponding sentence, and ensure the code runs at the right time in your survey flow.
Step-by-Step Solution
1. Set Up Embedded Data Fields
First, go to your survey's Survey Flow:
- Add an "Embedded Data" element at the very top (before any question blocks)
- Create fields named
word1,word2, ...,wordN(whereNis the number of sentences you have). For example, if you have 8 sentences, makeword1toword8.
2. Update the JavaScript (Run in Survey Flow)
Replace your existing code with this modified version, which will shuffle your word pool and assign each shuffled word to the embedded data fields you just created. Add this as a JavaScript action right after the Embedded Data element in the Survey Flow:
function shuffle(array) { let currentIndex = array.length, temporaryValue, randomIndex; while (0 !== currentIndex) { randomIndex = Math.floor(Math.random() * currentIndex); currentIndex -= 1; temporaryValue = array[currentIndex]; array[currentIndex] = array[randomIndex]; array[randomIndex] = temporaryValue; } return array; } // Replace this with your actual word pool const wordPool = ["WordA", "WordB", "WordC", "WordD", "WordE", "WordF", "WordG", "WordH"]; const shuffledWords = shuffle(wordPool); // Assign each shuffled word to the embedded data fields for (let i = 0; i < shuffledWords.length; i++) { // Note: We use i+1 here to match our "word1" to "wordN" naming Qualtrics.SurveyEngine.setEmbeddedData(`word${i+1}`, shuffledWords[i]); }
3. Link Words to Your Sentences
For each page that contains a sentence:
- Edit the text of the sentence, and insert the corresponding embedded data field using Qualtrics' pipe syntax. For example:
The ${e://Field/word1} jumped over the fence.
${e://Field/word2} is my favorite color.
4. Test the Survey
- Preview your survey and check two key things:
- The sentence order is the same every time you preview (matches your setup)
- Each sentence has a unique word, with no repeats across pages
- You can also verify the embedded data values by checking the "Response Summary" after a test submission—look for the
word1towordNfields to confirm they're populated with shuffled, unique words.
Bonus Tips
- If your word pool is larger than your number of sentences, add a slice to the shuffled array to only take the first N words (e.g.,
const shuffledWords = shuffle(wordPool).slice(0, 8);for 8 sentences) - Make sure the JavaScript runs before any question blocks—this ensures the embedded data is populated before the pages load
内容的提问来源于stack exchange,提问作者B.stb

