Counting Number of Text Entry Answers on Qualtrics Survey - javascript

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.

Related

assigning different value to javascript arrays created using new

I have written a webpage that helps in solving wordle using javascript.
It is similar to https://tryhardguides.com/wordle-solver/.
The difference is that my webpage can be run offline as all 2308 solutions and 12974 allowed guesses are saved in different array variables, also I allow multi letter input for yellow boxes.
Anyway, that was just a little explanation about purpose of my code.
My question for this thread is as follows:
I create global variable greenarray that holds a single letter guess for each position using
var greenarray=new Array(5).fill(0);
I am adding a reset button in it, so what would be the best way to fill greenarray with zeroes again? I believe that greenarray=new Array(5).fill(0); would leak 5 values I allocated in memory. Is that right? If so, then what is the appropriate way?
I am using another global variable yellowarray that holds multiple letter guesses for each position using
yarr = new Array(5);
for (var i = 0; i < yarr.length; i++)
{
yarr[i] = [];
}
Here I am pushing and popping letters to particular yarr[i] depending on user added characters or deleted previous ones to i'th position.
Here I think that resetting can be done by simply doing yarr[i]=[] as suggested at How to free up the memory in JavaScript. Is that ok?
Last thing, if somebody reloads the page then is memory allocated via new freed automatically?
Thanks.

Randomize question text using Javascript in Qualtrics

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.

Numbers from inside a string without regex?

I'm making a bot for a gaming chatroom with some friends, but I've hit an impasse. Is there a reliable way to get numbers from inside a string of text that won't completely break an inexperienced script kiddy's brain? Here's the best I've been able to come up with so far, variables simplified slightly for illustration's sake:
var k = [0];
function dieRoll(m,n) {
for(i = 0; i < m; i++) {
k[i] = Math.floor(Math.random()*n)+1;
}
}
var m = text[5];
var n = text[7];
if (text === 'roll '+m+'d'+n) {
dieRoll(m,n)
console.log(k);
}
The biggest problem as-is is that it's limited to single-digit input.
EDIT: Looping through the text looking for integers is exactly the kind of thing I'm looking for. I don't have much experience with programming, so I probably tend to end up with overly complicated and confusing messes of spaghetti code that would embarrass anyone remotely professional. As for the format of the input I'm looking for, "roll [number of dice]d[highest number on the dice]". For anyone who doesn't know, it's the notation most tabletop rpgs use. For example, "roll 2d6" for two normal six-sided dice.
EDIT: It's not that I'm necessarily against regex, I just want to be able to understand what's going on, so that if and when I need to edit or reuse the code it I can do so without going completely insane.
EDIT: Thank you all very much! split() seems to be exactly what I was looking for! It'll probably take some trial and error, but I think I'll be able to get her working how she's supposed to this weekend (Yes I call my bots 'she').
Basically, you need to look at the format of the input you're using, and identify certain facts about it. Here are the assumptions I've taken based on your question.
1) The "roll" command comes first followed by a space, and
2) After the command, you are provided with dice information in the form xdy.
Here's something that should work given those constraints:
function getRollParameters(inputCommand) {
var inputWords = inputCommand.split(' '); //Split our input around the space, into an array containing the text before and after the space as two separate elements.
var diceInfo = inputWords[1]; //Store the second element as "diceInfo"
var diceDetails = diceInfo.split('d'); //Split this diceInfo into two sections, that before and after the "d" - ie, the number of dice, and the sides.
//assign each part of the dicedetails to an appropriate variable
var dice = diceDetails[0];
var sides = diceDetails[1];
//return our two pieces of information as a convenient object.
return {
"dice": dice,
"sides": sides
};
}
//a couple of demonstrations
console.log(getRollParameters("roll 5d8"));
console.log(getRollParameters("roll 126d2"));
Effectively, we're first splitting the string into the "command", and the "arguments" - the information we want. Then, we split our arguments up using the "d" as a midpoint. That gives us two numbers - the one before and the one after the d. Then we assign those values to variables, and can use them however we like.
This obviously won't deal with more creative or flexible inputs, and isn't tested beyond the examples shown but it should be a decent starting point.

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