如何解决Qualtrics和Javascript-将单词随机插入句子
我进行了一项研究,提出了不同的句子(每页一个)。
我想在那些句子中随机插入单词。
每个参与者看到句子的顺序相同。每个句子中只有一个单词必须随机出现。
例如:
-你好[插入8个字之一]我喜欢狗。
-你好[插入剩余的7个单词之一]我喜欢鸟。
-嗨[插入剩下的6个单词之一]我喜欢猫。
等
我认为我必须使用Javascript,但我不知道该怎么做,但是也许有一个更简单的解决方案。
感谢您的帮助,祝您有美好的一天! (很抱歉,如果有错误,英语不是我的母语)。
编辑:
实际上,有人在Qualtrics论坛上建议了一个看似有效的代码,但我认为它有问题,因为它不起作用。
这是代码:
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]);
}
});
解决方法
嗨,这是开始的基本方法。
const quotes = [
"Hello $,I like dogs.","Hello $,I like cats.",I like doing my homework on my own and not asking for solutions on SO.",]
const names = ["B.stb","Donald","Julia"];
for (let quote of quotes) {
let name = names.splice(names.length * Math.random() | 0,1)[0]
console.log(quote.replace(/\$/g,name));
}
,
这是我使用生成器函数的不错方法
const words = ['a','b','c','d','e','f','g']
const randomize = function* (array) {
while (array.length != 0) {
const index = Math.floor(Math.random() * array.length)
yield array.splice(index,1)[0]
}
}
const sentenceResolvers = [
word => `hello ${word}`,word => `hey ${word}`,word => `yo ${word}`,]
const randomWords = randomize(words)
for (const resolver of sentenceResolvers) {
const iteration = randomWords.next()
if (iteration.done) {
throw new Error('not enough random words')
}
console.log(resolver(iteration.value))
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。