In one block I am collecting response from participant on a slider. And I am storing it as an embedded data. Now I want to generate two random numbers, one greater than the participants response and one lesser than the participants response. Then show them along with participant choice in the next block. Also, I want to do a mathematical operation on one of these random numbers.
I tried using Embedded data and random number but that does not always generate greater and lesser than the response of the participant. Is there any JS code available or similar code which I can modify. I am new in Java, so I need some help understanding.
Hi
Can you try the following code. Add the variables in the survey flow as “Lesser” “Greater” and “ParticipantResponse, include the code for the Slider question.
Qualtrics.SurveyEngine.addOnReady(function() {
var participantResponse = parseInt("${e://Field/ParticipantResponse}"); // Get stored response
if (!isNaN(participantResponse)) {
var lowerBound = Math.max(participantResponse - 10, 0); // Adjust range as needed
var upperBound = participantResponse + 10;
var lesser = Math.floor(Math.random() * (participantResponse - lowerBound)) + lowerBound;
var greater = Math.floor(Math.random() * (upperBound - participantResponse)) + participantResponse + 1;
Qualtrics.SurveyEngine.setEmbeddedData("Lesser", lesser);
Qualtrics.SurveyEngine.setEmbeddedData("Greater", greater);
// Example of a mathematical operation
Qualtrics.SurveyEngine.setEmbeddedData("ModifiedGreater", greater * 2);
}
});
Thank you


Thank you
// Generate numbers greater and lesser than participant's slider response
Qualtrics.SurveyEngine.addOnReady(function() {
// Get the current question object
var that = this;
// Function to calculate values and update embedded data
function calculateAndStore() {
// For Qualtrics discrete slider questions, we need a different approach to get the value
var participantResponse = null;
// Try multiple methods to get the slider value
// Method 1: Try to get the value directly from the question's input field
try {
var sliderEl = jQuery("#" + that.questionId + " .InputText");
if (sliderEl.length > 0) {
participantResponse = parseInt(sliderEl.val());
}
} catch (e) {
console.error("Error getting slider value from input field: " + e);
}
// Method 2: If Method 1 fails, try getting from selected choices
if (isNaN(participantResponse) || participantResponse === null) {
try {
// Get the selected choice index
var selectedIndex = that.getSelectedChoices() 0];
// Get the corresponding value from the choice list
// Since your slider has specific values (0, 25, 50, ...)
var values = h0, 25, 50, 75, 100, 125, 150, 175, 200, 225];
if (selectedIndex >= 0 && selectedIndex < values.length) {
participantResponse = values/selectedIndex];
}
} catch (e) {
console.error("Error getting value from choices: " + e);
}
}
// Method 3: Last resort - try to read the displayed value
if (isNaN(participantResponse) || participantResponse === null) {
try {
var displayedValue = jQuery("#" + that.questionId + " .track-label.active").text();
participantResponse = parseInt(displayedValue);
} catch (e) {
console.error("Error getting displayed value: " + e);
}
}
// Check if we have a valid number
if (!isNaN(participantResponse) && participantResponse !== null) {
// Define lower and upper bounds
var lowerBound = 0;
var upperBound = 225;
// Log the captured participant response
console.log("Captured Participant Response: " + participantResponse);
// Generate a random number less than or equal to participant's response
// This will be between lowerBound and participantResponse (inclusive)
var lesser = Math.floor(Math.random() * (participantResponse - lowerBound + 1)) + lowerBound;
// Generate a random number greater than or equal to participant's response
// This will be between participantResponse and upperBound (inclusive)
var greater = Math.floor(Math.random() * (upperBound - participantResponse + 1)) + participantResponse;
// Store original response and generated values as embedded data
Qualtrics.SurveyEngine.setEmbeddedData("ParticipantResponse", participantResponse);
Qualtrics.SurveyEngine.setEmbeddedData("Lesser", lesser);
Qualtrics.SurveyEngine.setEmbeddedData("Greater", greater);
// Perform mathematical operation (multiply greater by 2)
Qualtrics.SurveyEngine.setEmbeddedData("ModifiedGreater", greater * 2);
// Log all generated values for debugging
console.log("ParticipantResponse: " + participantResponse);
console.log("Lesser Value: " + lesser);
console.log("Greater Value: " + greater);
console.log("Modified Greater Value: " + (greater * 2));
} else {
// Log error if participant response is not a valid number
console.error("Invalid participant response value or no selection made");
}
}
// Calculate initially when page loads (if a value is already selected)
calculateAndStore();
// Update calculation when slider changes
this.questionclick = function(event, element) {
calculateAndStore();
}
// Make final calculation when page is submitted
Qualtrics.SurveyEngine.addOnPageSubmit(function() {
calculateAndStore();
});
});
This is the final code that worked for the above problem. I have the slider taking discrete values n0,225] in an increment of 25. The code is adjusted for that. Remember to create the Embedded data Fields “Lesser”, “Greater”, “ParticipantResponse” and “ModifiedGreater” before the block where the slider question is situated. And use these fields in the next blocks as you required.
Leave a Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.