Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

classification script

Hi, I would like to include in a survey classification tool. I have a free format question with 7 variables. Each variable has a number that represents the probability the respondents belongs to a given segment. Is there any function or java script I can use to identify which variable has a highest number?

Rafał
asked Feb 12 by RafalNeska (490 points)
Nothing too tricky about comparing values.  What are you hoping to do with the value?  If it's something on the same page, then we'll need to go the JavaScript route.  If it's something after the page, we'll need a Sawtooth Script solution.
I will route the respondent to a specific question depending on the segment.

1 Answer

0 votes
The easiest way would be to just write skips that check for that:

FreeFormatQ_var1 >= FreeFormatQ_var2 and FreeFormatQ_var1 >= FreeFormatQ_var3 and ...


This isn't the most efficient choice, though.  What I would probably do instead is add an additional variable to your free format question.  Then you can add this script to the free format question to automatically set this new variable to whichever variable has the highest value:

<script>
$(document).ready(function(){
    // Params
    var variables = ['var1', 'var2', 'var3', 'var4', 'var5', 'var6', 'var7'];
    var highestVariable = 'highest';
    
    // Run
    $('#[% QuestionName() %]_div input').keyup(function(){
        var highestIndex = 0;
        var highestValue = Number.MIN_VALUE;
        variables.forEach(function(variable, index){
            var value = SSI_GetValue('[% QuestionName() %]_' + variable);
            if (value > highestValue) {
                highestIndex = index;
                highestValue = value;
            }
        });
        $('#[% QuestionName() %]_' + highestVariable).val(highestIndex + 1);
    });
})
</script>


Line 4 should be updated with the names of your seven variables, as well as line 5 with the name of the new variable.
answered Feb 12 by Zachary Platinum Sawtooth Software, Inc. (171,950 points)
...