Javascript Help - Want to show respondents how their result deviated from the actual result | XM Community
Skip to main content

Hi Everyone,

My friend and I are currently creating a survey for our master project. To shortly describe the essence of the survey: we aim to provide the respondents with financial information (namely Revenue, Costs and Net Earnings), where they then need to predict the revenue and costs for the forthcoming year. Once they have done that, we would like the survey to display how far off they were (in percentage). We have tried to adjust the java script to incorporate it, but it just constantly says the answer deviated 100%. Do you know how to display how far off the respondents were once they have completed the survey?. We possess the unique IDs for input fields. However, it seems that it makes the deviation before the respondents have given their answer (hence the deviation of 100%).

 

The code (sorry for the Danish phrases):

Qualtrics.SurveyEngine.addOnReady(function() {
    // Define the actual numbers
    var actualOmsaetning = 100;
    var actualOmkostninger = -80;
    var actualAaretsResultat = 20;

    // Define the IDs for the constant sum input fields
    var omsaetningId = "QR~QID13~1~1";
    var omkostningerId = "QR~QID13~1~2";
    var aaretsResultatId = "QR~QID13~1~3";

    // Define the ID for the total row
    var totalRowId = "header~QID13~total";

    // Wait for the DOM to fully load before executing the script
    jQuery(document).ready(function() {
        // Get values from participants' responses
        var participantOmsaetning = parseFloat(jQuery("#" + omsaetningId + " input").val()) || 0;
        var participantOmkostninger = parseFloat(jQuery("#" + omkostningerId + " input").val()) || 0;
        var participantAaretsResultat = parseFloat(jQuery("#" + aaretsResultatId + " input").val()) || 0;

        // Calculate the percentage differences between participants' responses and actual numbers
        var diffOmsaetning = ((participantOmsaetning - actualOmsaetning) / Math.abs(actualOmsaetning)) * 100;
        var diffOmkostninger = ((participantOmkostninger - actualOmkostninger) / Math.abs(actualOmkostninger)) * 100;
        var diffAaretsResultat = ((participantAaretsResultat - actualAaretsResultat) / Math.abs(actualAaretsResultat)) * 100;

        // Store the differences in embedded data for later use
        Qualtrics.SurveyEngine.setEmbeddedData('resultMessage', 
            'Dine svar adskilte sig fra de faktiske værdier med følgende procentuelle differencer:\n' +
            'Omsætning: ' + diffOmsaetning.toFixed(2) + '%\n' +
            'Omkostninger: ' + diffOmkostninger.toFixed(2) + '%\n' +
            'Årets Resultat: ' + diffAaretsResultat.toFixed(2) + '%'
        );

        // Identify the total row element by its ID or class
        var totalRow = document.getElementById(totalRowId);

        // Check if the element is found
        if (totalRow) {
            // Change the label text
            totalRow.innerHTML = 'Årets resultat';
        }
    });
});

 

Thank you!

 

Kind regards,

Daniel & Carl-Gustav

 

 

Be the first to reply!

Leave a Reply