Coding Help | XM Community
Skip to main content

So I am trying to calculate a percentile and t score for two separate categories within one question. This is the code I have. However, no matter the answers I input I get the same t-score and percentile so clearly I am doing something wrong.

 

Qualtrics.SurveyEngine.addOnReady(function() {
    // Convert Likert scale responses to numerical values
    function likertToNumeric(value) {
        const scale = {
            "Never": 1,
            "Almost never": 2,
            "Sometimes": 3,
            "Often": 4,
            "Almost always": 5
        };
        return scale value] || 0; // default to 0 if value not found
    }

    // Calculate sum of responses
    function calculateSum(variables) {
        return variables.reduce(function(accumulator, current) {
            var response = Qualtrics.SurveyEngine.getEmbeddedData(current);
            console.log(current + ": " + response); // Debugging: Log each response value
            return accumulator + likertToNumeric(response);
        }, 0);
    }

    // Calculate T-score
    function calculateTScore(sum, mean, sd) {
        var tScore = 50 + 10 * ((sum - mean) / sd);
        console.log("Calculated T-Score: " + tScore); // Debugging: Log calculated T-Score
        return tScore;
    }
    function mapTscoreToPercentile(tScore) {
        if (tScore >= 66) return 95;
        if (tScore == 65) return 94;
        if (tScore == 64) return 92;
        if (tScore == 63) return 91;
        if (tScore == 62) return 88;
        if (tScore == 61) return 87;
        if (tScore == 60) return 84;
        if (tScore == 59) return 82;
        if (tScore == 58) return 79;
        if (tScore == 57) return 77;
        if (tScore == 56) return 73;
        if (tScore == 55) return 70;
        if (tScore == 54) return 66;
        if (tScore == 53) return 61;
        if (tScore == 52) return 58;
        if (tScore == 51) return 55;
        if (tScore == 50) return 50;
        if (tScore == 49) return 45;
        if (tScore == 48) return 42;
        if (tScore == 47) return 39;
        if (tScore == 46) return 34;
        if (tScore == 45) return 32;
        if (tScore == 44) return 27;
        if (tScore == 43) return 25;
        if (tScore == 42) return 21;
        if (tScore == 41) return 19;
        if (tScore == 40) return 16;
        if (tScore == 39) return 14;
        if (tScore == 38) return 12;
        if (tScore == 37) return 10;
        if (tScore == 36) return 8;
        if (tScore == 35) return 7;
        if (tScore == 34) return 5;
        if (tScore == 33) return 5;
        if (tScore == 32) return 4;
        if (tScore == 31) return 3;
        if (tScore == 30) return 2;
        if (tScore == 29) return 2;
        if (tScore <= 28) return 1;
    }

    var firstGroup = 'stress24', 'piling24', 'overwh24', 'unable24'];
    var secondGroup = 'throw24', 'staym24', 'yell24', 'angry24'];
    var firstGroupMean = 12.5740;
    var firstGroupSD = 4.73907;
    var secondGroupMean = 10.5049;
    var secondGroupSD = 4.66062;

    // Calculate sums, T-scores, and Percentiles
    var firstSum = calculateSum(firstGroup);
    var secondSum = calculateSum(secondGroup);
    var firstTScore = calculateTScore(firstSum, firstGroupMean, firstGroupSD);
    var secondTScore = calculateTScore(secondSum, secondGroupMean, secondGroupSD);
    var firstPercentile = mapTscoreToPercentile(firstTScore);
    var secondPercentile = mapTscoreToPercentile(secondTScore);

    // Set embedded data
    Qualtrics.SurveyEngine.setEmbeddedData('pstresst', firstTScore);
    Qualtrics.SurveyEngine.setEmbeddedData('pstressp', firstPercentile);
    Qualtrics.SurveyEngine.setEmbeddedData('angert', secondTScore);
    Qualtrics.SurveyEngine.setEmbeddedData('angerp', secondPercentile);
});

@avimintz Some ideas…

  • Ensure that the embedded data is set correctly in Qualtrics before running the code. If `Qualtrics.SurveyEngine.getEmbeddedData(current)` isn't retrieving valid responses, the calculations will be based on `0` values, leading to the same score and percentile.
  • The `console.log` statements should help you debug. Check the browser's console to see if the values retrieved by `Qualtrics.SurveyEngine.getEmbeddedData(current)` are correct.
  • The `likertToNumeric` function has a fallback of `0` if the value is not found in the scale. If the input values don't match exactly with the keys (`"Never"`, `"Almost never"`, etc.), the function will return `0`, causing incorrect sum calculations. Consider adding logging or checking the input values.
  • Ensure that the variables in `firstGroup` and `secondGroup` are correctly named and that they correspond to actual embedded data fields in the survey. Typos or mismatches in variable names would result in `undefined` responses.
  • Ensure that the survey flow logic correctly assigns values to the embedded data before this JavaScript runs. Otherwise, you might end up with default or empty values. e.g., for the emebdded data fields you try to retrieve with JavaScript: stress24, ...

BR

Christian


@avimintz This code contains minor improvements and additional logging:

Qualtrics.SurveyEngine.addOnReady(function() {
// Convert Likert scale responses to numerical values
function likertToNumeric(value) {
const scale = {
"Never": 1,
"Almost never": 2,
"Sometimes": 3,
"Often": 4,
"Almost always": 5
};
if (scaleevalue] !== undefined) {
return scaleevalue];
} else {
console.log("Unexpected value: " + value);
return 0; // default to 0 if value not found
}
}

// Calculate sum of responses
function calculateSum(variables) {
return variables.reduce(function(accumulator, current) {
var response = Qualtrics.SurveyEngine.getEmbeddedData(current);
console.log(current + ": " + response); // Debugging: Log each response value
return accumulator + likertToNumeric(response);
}, 0);
}

// Calculate T-score
function calculateTScore(sum, mean, sd) {
var tScore = 50 + 10 * ((sum - mean) / sd);
console.log("Calculated T-Score: " + tScore); // Debugging: Log calculated T-Score
return tScore;
}

function mapTscoreToPercentile(tScore) {
if (tScore >= 66) return 95;
if (tScore == 65) return 94;
if (tScore == 64) return 92;
if (tScore == 63) return 91;
if (tScore == 62) return 88;
if (tScore == 61) return 87;
if (tScore == 60) return 84;
if (tScore == 59) return 82;
if (tScore == 58) return 79;
if (tScore == 57) return 77;
if (tScore == 56) return 73;
if (tScore == 55) return 70;
if (tScore == 54) return 66;
if (tScore == 53) return 61;
if (tScore == 52) return 58;
if (tScore == 51) return 55;
if (tScore == 50) return 50;
if (tScore == 49) return 45;
if (tScore == 48) return 42;
if (tScore == 47) return 39;
if (tScore == 46) return 34;
if (tScore == 45) return 32;
if (tScore == 44) return 27;
if (tScore == 43) return 25;
if (tScore == 42) return 21;
if (tScore == 41) return 19;
if (tScore == 40) return 16;
if (tScore == 39) return 14;
if (tScore == 38) return 12;
if (tScore == 37) return 10;
if (tScore == 36) return 8;
if (tScore == 35) return 7;
if (tScore == 34) return 5;
if (tScore == 33) return 5;
if (tScore == 32) return 4;
if (tScore == 31) return 3;
if (tScore == 30) return 2;
if (tScore == 29) return 2;
if (tScore <= 28) return 1;
}

var firstGroup = 'stress24', 'piling24', 'overwh24', 'unable24'];
var secondGroup = 'throw24', 'staym24', 'yell24', 'angry24'];
var firstGroupMean = 12.5740;
var firstGroupSD = 4.73907;
var secondGroupMean = 10.5049;
var secondGroupSD = 4.66062;

// Calculate sums, T-scores, and Percentiles
var firstSum = calculateSum(firstGroup);
var secondSum = calculateSum(secondGroup);
console.log("First Sum: " + firstSum); // Debugging: Log calculated sum
console.log("Second Sum: " + secondSum); // Debugging: Log calculated sum

var firstTScore = calculateTScore(firstSum, firstGroupMean, firstGroupSD);
var secondTScore = calculateTScore(secondSum, secondGroupMean, secondGroupSD);

var firstPercentile = mapTscoreToPercentile(firstTScore);
var secondPercentile = mapTscoreToPercentile(secondTScore);

// Set embedded data
Qualtrics.SurveyEngine.setEmbeddedData('pstresst', firstTScore);
Qualtrics.SurveyEngine.setEmbeddedData('pstressp', firstPercentile);
Qualtrics.SurveyEngine.setEmbeddedData('angert', secondTScore);
Qualtrics.SurveyEngine.setEmbeddedData('angerp', secondPercentile);
});

Best
Christian


Thank you so much! When I used it, for some reason it still is not working. My console says ReferenceError: stress24 is not defined SE API Error: ReferenceError: choiceId1 is not defined function(type) { even though the name of the variable is stress24 (attached below):

 


@avimintz Does it indicate from which line the error results? I can only imagine that it belongs to this:

var response = Qualtrics.SurveyEngine.getEmbeddedData(current);

This line expects some embedded data called stress24 (same for all other values in firstGroup and secondGroup). Having the question export tags defined is not sufficient.

Do you try to get the selected recode value (1-5) for stress24 and the others with this line of code to sum them in calculateSum? Guess this must be done differently. What question type do you use, Matrix?


Leave a Reply