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);
});