You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Qualtrics结合JavaScript实现句子随机插入不重复词汇的技术求助

How to Insert Unique Random Words into Fixed-Order Sentences in Qualtrics

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 (where N is the number of sentences you have). For example, if you have 8 sentences, make word1 to word8.

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]);
}

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:
    1. The sentence order is the same every time you preview (matches your setup)
    2. 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 word1 to wordN fields 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 06:52:33