Randomize question text using Javascript in Qualtrics - javascript

I am trying to setup a survey in Qualtrics and want to randomize each question in the following way.
Every question asks the participant to make a choice, based on two variable parameters - Type of opponent (T) and Action (A). There are 6 types of opponent (T1,T2...T6) and 3 types of actions (A1,A2,A3). The actions have 6 replicates each (meaning there are 6 actions each of type A1,A2 and A3 A1[1..6],A2[1..6],A3[1..6]). There are 18 actions in total.
The question text will read
"You are playing against T[i] who has taken action Aj[k] "
i=1,2..6
j=1,2,3
k=1,2..6
I want to randomly assign the 6 questions in each action type to the 6 types of opponents - thereby generating 18 questions per subject.
How do i make the question text variable?
I have tried placing the following within the
Qualtrics.SurveyEngine.addOnReady(function(){})
block as a first step to at least randomly generate one of the opponent types, but the text does not change at all.
I am a complete newbie to javascript, although I have other coding experience.
Qualtrics.SurveyEngine.addOnReady(function()
{
var opponent_types =["Red","Blue","Green","Yellow"];
var selected_opponent_type = opponent_types[Math.floor(Math.random()*opponent_types.length)];
this.QuestionText = selected_opponent_type;
})
I am sure this is a syntax issue, can someone please point me in the right direction to go about this? The document.write is disabled in the Qualtrics API.

Related

Simple popular/trending products algorithm for an ecommerce site with Javascript

I am currently building a small ecommerce site and for the home page, I would like to rank the products based on popularity. The product schema has the following data:
{
...
noViews: Number, // Number of times a user has clicked on the product
avgRating: Number, // Average star rating (1 - 5)
datePosted: Date // Date the product was posted
}
What is a (relatively) simple algorithm/code I can use to implement this (with Node.js), where the products would be ranked based on popularity (with time taken into consideration so the content isn't stale).
Thank you.
First of all, you need a time based metric to solve this.
Overall trending products does not make much sense particularly for a ecommerce app.
Lets say, you want to do find the trending products for a particular day (say today).
Now this is not a easy problem to solve. And there is no just 1 direct way to solve it. For each company, one method works!
Ideally , what could be a simple solution here would be to create a score value based on combination of factors. For example, number of buys, number of views, number of clicks and so on.
Here, you have the datePosted parameter. Now, create a date_score for this. So, the basic idea here is a product that was posted yesterday has more score than the one posted a week back. The values you put need to tweaked and checked for your algorithm's customization.
Similarly, implemeent a similar score for avgRating and noViews.
Once you have these scores ready, create weights for these scores. Now the weighted average for these out of 100 is the final score.
So, a final example solution:
Date Posted:
If Date is in the last 3 days (score = 100)
If Date is the last week (score= 75)
If Date in the previous week (score = 50)
Else default score = 25.
Star Rating:
(Rating/5 ) *100
numberOfViews:
Here , you can use percentiles.
Lets say max views among all the product is x.
Then, the view score is (views/x) *100
Now, handle the corner case of what happens when two scores match.
After this, you can simply order by the score parameter.
Also, make sure everything you do is dynamic as static thresholds dont provide a great resul generally!

Detect Complex Dice Formats

I am currently working on a program that scans a page and highlights dice rolls and then lets you click to rolls those dice.
The program mostly works as expected, for example if the page contains stuff like 1d6, 1d6 + 3, d20, or 1d6 x 100 it picks up and rolls it. However I would like the ability to detect more complicated roles for example 1d20 + 4 1d12 + 12 and then parse that out into its individual roles. I would like to avoid detecting things like 1d20 + 4 the 1d12 + 12, basically, avoid things that have words instead of spaces in the dice roll.
My script uses regex to match patterns on the page and highlight them. I am not sure if this is something I could add to my regex since this is really been my project for learning regex. Here is the current regex I am using
([+−-]\d+)|(([1-9]\d*)?d([1-9]\d*)\s*([+-−]\s*\d+)?([,]\s*\d+)?)
And a sample on https://regexr.com/3tl1o
I think this is what you want
((?:[+-]?[1-9]\d*)?d[1-9]\d*(?:\s+[-+x]\s+[1-9]\d*)?(?:\s+(?:[+-]?[1-9]\d*)?d[1-9]\d*(?:\s+[+-x]\s+[1-9]\d*))*)
See https://regex101.com/r/Ap0HZw/1 for the explination

Counting Number of Text Entry Answers on Qualtrics Survey

I am entirely new to both Qualtrics and Javascript (as such, apologies for how elementary this question may be). I have a survey on the former that will require using Javascript in a couple of the different Blocks in the survey to count the number of answers provided by a user in a number of different text entry boxes (all of which are in just one Block).
We want to pay respondents by the number of suggestions they make, so I really just want to count how many of the different text entry boxes in that one question Block have something written in them, to then use as a variable in another Block at the end of the survey to pay respondents based on how many questions they answered.
Conceptually, I assume it'll just mean initializing a counter, looping over the N text entry boxes we have, adding +1 to the counter for every one of those N boxes in which anything has been entered (I'm not worrying about whether what is entered is actually meaningful to us, assuming people don't just enter spaces), and keeping that variable to use in the very last block of the survey (where we tell them what they have earned, which is a fixed amount per suggestion). I just can't seem to find even the building blocks/ elementary syntax to implement that very simple counter through Googling and searching around here (I tried to get a bit of inspiration from these questions but no success so far: (1) Qualtrics Word Counter Javascript; (2) Qualtrics: javascript - text entry).
Any help would be appreciated!
You can do this in your survey flow after the text entry questions block:
Embedded Data: count = 0
Branch: If Text Question 1 Not Empty
Embedded Data: count = $e{ e://Field/count + 1 }
Branch: If Text Question 2 Not Empty
Embedded Data: count = $e{ e://Field/count + 1 }
etc...
At the end count will equal the number of text entry questions answered.

Generating random number that will not repeat between surveys in Qualtrics using javascript

A disclaimer: I'm not familiar with Javascript. I've merely cobbled together a basic understanding of what I need to do for this task from Stack Overflow and other resources. My apologies if something below is unclear.
My problem: I need to generate a random number between 0 and 8,764, using Javascript, that will not repeat itself between Qualtrics survey responses.
Currently, I've found code to create an array that contains all numbers between 0 and 8,764, shuffles the array, and pops the last number off the end of the array.
It then adds embedded data to Qualtrics with that popped number, and I can then pipe the embedded data into a Qualtrics question to display it to my survey respondent. See below:
Qualtrics.SurveyEngine.addOnReady(function()
{
for (var i = 0, ar = []; i < 8; i++) {
ar[i] = i;
}
ar.sort(function () {
return Math.random() - 0.5;
});
var randomnumber = ar.pop();
Qualtrics.SurveyEngine.addEmbeddedData("randomnumber", randomnumber);
});
However, as far as I can tell, this Javascript code "resets" itself between survey responses, meaning it will re-create and re-shuffle the array each time a new respondent enters the survey. I'd like to find a way to make it so that it will be impossible for a new respondent to see the same popped "randomnumber" as a previous respondent. So, if the first survey respondent saw a 1, then the next survey respondent could see any number besides a 1 (let's say they see a 100 instead), and the next respondent could see any number except a 1 or a 100, etc etc.
I think it's possible to use embedded data in Javascript code and manipulate it (see here). It seems like there might be a way to access the randomnumber embedded data and write Javascript code to not remove any numbers from the array that match one of the previously popped randomnumbers. I lack the technical knowledge to execute this, if it's even the best way to accomplish the task.
Any and all help appreciated!
You can do what you want with Advanced Randomization in Qualtrics.
Set up a multiple choice question with your numbers 0 through 8,764 as the choices. Then use Advanced Randomization to select a random subset of 1 from all the numbers and click "Evenly Present" (Evenly Present is what tells Qualtrics to use every number before reusing any). Use JavaScript to hide the multiple choice question:
$(this.questionId).hide();
Now you can pipe your unique random number into a subsequent question. For example:
${q://QID1/ChoiceGroup/DisplayedChoices}

Qualtrics scoring Loop & Merge questions

I'm setting up a simple Qualtrics survey with one question with a Loop & Merge function (in this one block); in Loop & Merge field 1, I've provided the URLs to my media files. The respondents have to select the right answer from two answer options (let's say Yes/No). All all my files are set up as a Loop & Merge within one question so that I won't have to create 100 separate questions for each individual media file.
This works great, however, I would also like to score my respondents' answers. The regular "scoring" feature in Qualtrics doesn't seem to work for me, since I can only provide one scoring option per question (i.e. I could only say that the first answer is always 1 point, and the second answer is always 0 points). However, the correct answer varies between my files (sometimes it's Yes/the first option; sometimes it's No/the second option).
I'm thinking there might be a way to list the correct answer (=i.e. the answer that should receive 1 point) in Field 2 in the Loop & Merge function; and then include some (javascript?) code in the question which would check the survey taker's answer choice against the "correct answer" in Field 2 of the Loop & Merge function for each media file. The code would assign "1" point if the participant's selected answer corresponds to Field 2 for each media file.
How would I write the (javascript) code to calculate a score for each question and an overall score at the end? (I don't need survey takers to see their score, but once a person is done, I would like to quickly see what their final overall score is, say 72 out of 100 possible points.)
Update: In the loop/merge function, I've added the right answer (for each file) in Field2, the incorrect answer in Field3. As suggested below, I've piped loop fields (2 and 3) into my question choices. I've added code in Field4 about whether or not the order of the answer options should be changed (0,1; 0= don't change order, 1= change order) so that the options always occur in the same order (for example, always Choice 1 = "Yes", Choice 2 = "No".) I've (unsuccessfully) tried to use the following JS code to refer to Field 4:
if (${lm://Field/4}==1) {
(choiceNum = ${lm://Field/3}, ${lm://Field/2})
}
If there is a way to work without JavaScript, I'd be glad to hear about that option as well.
Thank you so much!
How about this without JavaScript:
Include your two choices as loop fields: Field 2 is correct answer, Field 3 is incorrect answer
Pipe your loop fields (2 and 3) into your question choices
Randomize the choices
Score the question: Choice 1/Field 2 = 1 point, Choice 2/Field 3 = 0 points.

Categories