I want to create a survey that displays the previous choices and calculates score based on choices | XM Community
Skip to main content

Q1. Displays a random number from 1 to 10. I figured this out!

Q2. Select the number from 1 to 10. 

Q3. Displays selected number and points earned.

Points Earned is 10 if selected number = random number. If not 10, it’s the difference of selected number - random number.

I tried using JavaScript, Piped Text, Embedded Data but it doesn’t work.

 

JavaScript for Question 2.

Qualtrics.SurveyEngine.addOnload(function() {
    // Get the value of the selected radio button in Question 2
    var selectedNumber = jQuery("#"+this.Q2+" inputQtype='radio']:checked").val();
    console.log("Selected Number: " + selectedNumber);
    // Set the "SelectedNumber" embedded data field to the captured value
    Qualtrics.SurveyEngine.setEmbeddedData('SelectedNumber', selectedNumber);
});


Qualtrics.SurveyEngine.addOnReady(function()
{
    /*Place your JavaScript here to run when the page is fully displayed*/

});

Qualtrics.SurveyEngine.addOnUnload(function()
{
    /*Place your JavaScript here to run when the page is unloaded*/

});

 

Defined Embedded Data

SelectedNumber

 

Inserted Piped Text in Q3 - Your selected number was ${e://Field/SelectedNumber}

Doesn’t work. I can’t even display the SelectedNumber let alone calculate the points. Please help!

Before Q3 you can use survey flow to do branch logic i.e. first assign embedded data score  = 10 then check if number entered and display are not equal in branch logic, if yes the using Math Operation do the subtraction and update the embedded data score to the math operation result. 


I’m unable to fetch SelectedNumber and PointsScored.

 

Could you help? Here’s my survey preview.

https://illinois.qualtrics.com/jfe/preview/previewId/8eb0851a-85b0-41e1-89b0-883173619dbc/SV_1zEi7ArtY82x5rw?Q_CHL=preview&Q_SurveyVersionID=current


I completed it! I had to change the code to
 

Qualtrics.SurveyEngine.addOnload(function()
{
// Fetch the random number generated from Embedded Data
var randomNumber = parseInt("${e://Field/RandomNumber}");

console.log('Random Number:', randomNumber);

function calculateScore(randomNumber, selectedNumber) {
    if (randomNumber === selectedNumber) {
        return 10;  // If the random number is equal to the selected number, assign a score of 10
    } else {
        return 10 - Math.abs(randomNumber - selectedNumber);  // Calculate the score based on the difference
    }
}

// Trigger the calculation when the participant selects an answer
jQuery('#QID1 input').change(function() {
    // Parse the selected number
    var selectedNumber = parseInt(jQuery(this).val());
    selectedNumber = selectedNumber - 6;

    // Calculate the score
    var score = calculateScore(randomNumber, selectedNumber);
    
    // Set the score as embedded data
    Qualtrics.SurveyEngine.setEmbeddedData('score', score);

    // Log the results
    console.log('Selected Number:', selectedNumber);
    console.log('Calculated Score:', score);
});


});

Qualtrics.SurveyEngine.addOnReady(function()
{
    /*Place your JavaScript here to run when the page is fully displayed*/

});

Qualtrics.SurveyEngine.addOnUnload(function()
{
    /*Place your JavaScript here to run when the page is unloaded*/

});


Leave a Reply