Sorting an array from selected recoded data | XM Community
Skip to main content

I need some help with code that doesn’t seem to be working. I am trying to create a ranked list of situations based on the participants likert scale rating. But only when the participant meets certain other criteria.

I have defined in the survey flow the other variables in the code (eg., OCLASS, OGROUNDS). And also defined the variables for the ranked list. I have pieced it together from other examples. Any suggestions would be welcomed.


Qualtrics.SurveyEngine.addOnload(function() {
    const QID1648_SelectedChoicesRecode = "some_value";
    const QID1656_SelectedChoicesRecode = "another_value";
    const QID1667_SelectedChoicesRecode = "another_value";
    const QID1655_SelectedChoicesRecode = "another_value";
    const QID1652_SelectedChoicesRecode = "another_value";
    const QID1664_SelectedChoicesRecode = "another_value";
    const QID1665_SelectedChoicesRecode = "another_value"; 
    const QID1649_SelectedChoicesRecode = "another_value";
    const QID1670_SelectedChoicesRecode = "another_value";
    const QID1671_SelectedChoicesRecode = "another_value";
    const QID1661_SelectedChoicesRecode = "another_value";
    const QID1662_SelectedChoicesRecode = "another_value";
    const QID1698_SelectedChoicesRecode = "another_value";
    const QID1663_SelectedChoicesRecode = "another_value";
    const QID1658_SelectedChoicesRecode = "another_value";
    const QID1659_SelectedChoicesRecode = "another_value";
    const QID1660_SelectedChoicesRecode = "another_value";
    const QID1650_SelectedChoicesRecode = "another_value";
    const QID1651_SelectedChoicesRecode = "another_value";

    let SOCLASS = null;
    let SOGROUNDS = null;
    let SOPLAY = null;
    let SOSCHOOL = null;
    let SOASCHOOL = null;
    let SOFRIEND = null;
    let SOPARTY = null;
    let SSBED = null;
    let SSALONE = null;
    let SSWAKE = null;
    let SSLIGHT = null;
    let SSFAMILY = null;
    let SSFRIEND = null;
    let SSCAMP = null;
    let SHBEDROOM = null;
    let SHBATHROOM = null;
    let SHCOMMON = null;
    let SHFAMILY = null;
    let SHSITTER = null;
   
    if (OCLASS === 1) {
        SOCLASS = QID1648_SelectedChoicesRecode;
    }
    if (OGROUNDS === 1) {
        SOGROUNDS = QID1656_SelectedChoicesRecode;
    }
    if (OPLAY === 1) {
        SOPLAY = QID1667_SelectedChoicesRecode;
    }
    if (OSCHOOL=== 1) {
        SOSCHOOL = QID1655_SelectedChoicesRecode;   
    }
    if (OASCHOOL === 1) {
        SOASCHOOL = QID1652_SelectedChoicesRecode;
    }
    if (OFRIEND === 1) {
        SOFRIEND = QID1664_SelectedChoicesRecode;
    }
    if (OPARTY === 1) {
        SOPARTY = QID1665_SelectedChoicesRecode;
    }
    if (SBED === 1) {
        SSBED = QID1649_SelectedChoicesRecode;
    }
    if (SALONE === 1) {
        SSALONE = QID1670_SelectedChoicesRecode;
    }
    if (SWAKE === 1) {
        SSWAKE = QID1671_SelectedChoicesRecode;
    }
    if (SLIGHT === 1) {
        SSLIGHT = QID1661_SelectedChoicesRecode;
    }
    if (SFAMILY === 1) {
        SSFAMILY = QID1662_SelectedChoicesRecode;
    }
    if (SFRIEND === 1) {
        SSFRIEND = QID1698_SelectedChoicesRecode;
    }    
    if (SCAMP === 1) {
        SSCAMP = QID1663_SelectedChoicesRecode;
    }
    if (HBEDROOM === 1) {
        SHBEDROOM = QID1658_SelectedChoicesRecode;
    }
    if (HBATHROOM === 1) {
        SHBATHROOM = QID1659_SelectedChoicesRecode;
    }
    if (HCOMMON === 1) {
        SHCOMMON = QID1660_SelectedChoicesRecode;
    }
    if (HFAMILY === 1) {
        SHFAMILY = QID1650_SelectedChoicesRecode;
    }
    if (HSITTER === 1) {
        SHSITTER = QID1651_SelectedChoicesRecode;
    }
    
    var separation = >
        { name: 'SOCLASS', value: parseInt("${e://Field/SOCLASS}") },
        { name: 'SOGROUNDS', value: parseInt("${e://Field/SOGROUNDS}") },
        { name: 'SOPLAY', value: parseInt("${e://Field/SOPLAY}") },
        { name: 'SOSCHOOL', value: parseInt("${e://Field/SOSCHOOL}") },
        { name: 'SOASCHOOL', value: parseInt("${e://Field/SOASCHOOL}") },
        { name: 'SOFRIEND', value: parseInt("${e://Field/SOFRIEND}") },
        { name: 'SOPARTY', value: parseInt("${e://Field/SOPARTY}") },
        { name: 'SHBEDROOM', value: parseInt("${e://Field/SHBEDROOM}") },
        { name: 'SHBATHROOM', value: parseInt("${e://Field/SHBATHROOM}") },
        { name: 'SHCOMMON', value: parseInt("${e://Field/SHCOMMON}") },
        { name: 'SHFAMILY', value: parseInt("${e://Field/SHFAMILY}") },
        { name: 'SHSITTER', value: parseInt("${e://Field/SHSITTER") },
        { name: 'SSBED', value: parseInt("${e://Field/SSBED}") },
        { name: 'SSALONE', value: parseInt("${e://Field/SSALONE}") },
        { name: 'SSWAKE', value: parseInt("${e://Field/SSWAKE}") },
        { name: 'SSLIGHT', value: parseInt("${e://Field/SSLIGHT}") },
        { name: 'SSFAMILY', value: parseInt("${e://Field/SSFAMILY}") },
        { name: 'SSCAMP', value: parseInt("${e://Field/SSCAMP}") },
        { name: 'SSFRIEND', value: parseInt("${e://Field/SSFRIEND}") },
    ];

    separation.sort(function(a, b) {
        return b.value - a.value;
    }); 

    Qualtrics.SurveyEngine.setEmbeddedData( '1', separation 0].name);
    Qualtrics.SurveyEngine.setEmbeddedData( '2', separation1].name);
    Qualtrics.SurveyEngine.setEmbeddedData( '3', separation 2].name);
    Qualtrics.SurveyEngine.setEmbeddedData( '4', separatione3].name);
    Qualtrics.SurveyEngine.setEmbeddedData( '5', separatione4].name);
    Qualtrics.SurveyEngine.setEmbeddedData( '6', separationi5].name);
    Qualtrics.SurveyEngine.setEmbeddedData( '7', separationn6].name);
    Qualtrics.SurveyEngine.setEmbeddedData( '8', separationy7].name);
    Qualtrics.SurveyEngine.setEmbeddedData( '9', separationv8].name);
    Qualtrics.SurveyEngine.setEmbeddedData( '10', separationr9].name);
    Qualtrics.SurveyEngine.setEmbeddedData( '11', separationu10].name);
    Qualtrics.SurveyEngine.setEmbeddedData( '12', separationu11].name);
    Qualtrics.SurveyEngine.setEmbeddedData( '13', separationu12].name);
    Qualtrics.SurveyEngine.setEmbeddedData( '14', separationu13].name);

});

Apologies. This is a duplicate post. 


Leave a Reply