Hi all -
I have a 4x3 matrix question where respondents enter three numerical values in each of four rows of open text fields and I need to calculate the median value in each row. Qualtrics doesn't appear to include a median math operation and scoring isn't available for this question type.
TomG suggested the below code to someone who needed to calculate a max value and I'd love something similarly simple if it exists. (Unfortunately Math.median doesn't appear to be a thing. :/) Thanks in advance!
var max = Math.max(parseInt("${q://QID16ChoiceTextEntryValue"), parseInt("${q://QID17ChoiceTextEntryValue}"), parseInt("${q://QID18ChoiceTextEntryValue)}"));
Qualtrics.SurveyEngine.setEmbeddedData("max", max);
Hi VirginiaUKY,
You can create your own median function for ease of use :)
Example stolen from StackOverflow with some annotations of my own.
//defining the func
function getMedian(data) {
const values = [...data];
//sort the values
const v = values.sort( (a, b) => a - b);
//find the middle
const mid = Math.floor( v.length / 2);
//calculate the median
const median = (v.length % 2 !== 0) ? v[mid] : (v[mid - 1] + v[mid]) / 2;
return median;
}
//calling the func - change the IDs to whatever is correct
var val1 = "${q://QID1ChoiceTextEntryValue}"
var val2 = "${q://QID2ChoiceTextEntryValue}"
var val3 = "${q://QID3ChoiceTextEntryValue}"
Qualtrics.SurveyEngine.setEmbeddedData("median", getMedian(tval1,val2,val3]))
Good luck!
Thanks! I'll give that a shot.
Leave a Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.