Have an idea?

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

CBC question: Display a number using a formula with inputs from different attributes

Hi all,

I'm trying to program a CBC exercise in which I will display a sentence summarizing the situation, instead of showing the different attributes and their level separately.

For example, if I have three attributes -- Average # of Items Bought in Each Purchase (n), Average Cost per Item (c), and Total # of Purchases made (p) -- and I want to show something like,

"You would spend an estimated $ (n X c X p) on this." on each card (i.e., concept) instead of the usual set up like below.

                                                             Card 1                      Card 2                   Card 3

Average # Item Bought                n*                                n*                           n*

Average Cost per Item                  c*                                 c*                           c*                         

Total # of Purchases                      p*                                p*                           p*

*specific level generated by the Sawtooth design.

Is there a way to do this with unverified perl (or some other methods), instead of hard coding the display for each combination of the attributes using the conditional relationship function? In my case, I have 5 attributes and 250 total different combinations of attribute levels, so I'd love to avoid doing the hard coding if possible.

Thank you very much in advance for your help!
asked Oct 15, 2021 by Yixian
It sounds like something we can handle with JavaScript.  Can you clarify exactly how you want the final text to look?  You mention having five attributes, but you only show three in your example.  Are we hiding these three attributes but keeping the other two then?

It sounds like you just want to multiply the three values.  Are we just printing out the result or are we including the original three values in there as well?  If the original three values aren't being displayed, I'd want to wrap in an analytics expert to verify that doing so won't have any negative effects on the data.
Also, what version of SSI Web / Lighthouse Studio are you running?
Thank you very much for the quick response Zach!  

The final text that I was aiming for would be something like "You will be paying (...) times for $(X) each time, with an interest rate of (...), and a fee of (...)".

I have five attributes but was using three in the example just to make it a bit easier to explain. Ideally I was hoping to not display any of the five attributes, but just the final text to the respondents, but please let me know if that's going to cause trouble with the data.

The actual calculation is a complicated formula with inputs from all the five attribute levels, I was using multiplication to simplify the explanation. The X value in the above statement is the value that will be calculated with all the five attribute inputs.

I'm using version 9.11 of lighthouse.

Thanks again very much!

1 Answer

0 votes
What I mean is not whether the attributes are literally shown as separate rows of the CBC, but whether or not respondents are able to understand all the key aspects of each concept.  For example, imagine if you had two attributes and your calculation was to multiply them together.  A concept with values 4 and 4 would produce the same calculated result as a concept with values 2 and 8.  If you were to just show the score of 16 in both concepts, respondents would have no idea what the underlying difference is.  But if you show the calculated value as well as the level values like it looks like you plan to, the confusion goes away.

I would start by adding this HTML to each level of the CBC:

<input type="hidden" class="att1" value="100"/>


The "att1" should be updated with the attribute number and the "100" should be updated with the value for this level.  You may want to apply an internal label so this HTML doesn't show up in analysis.

Then I'd use a merged row to combine all attributes.

Finally, I'd use this JavaScript to do the work:

<script>
$(document).ready(function(){
    $('.merged_text').each(function(){
        // Read levels
        var att1 = Number($(this).find('.att1').val());
        var att2 = Number($(this).find('.att2').val());
        var att3 = Number($(this).find('.att3').val());
        
        // Calculations
        var product = att1 * att2 * att3;
        
        // Print
        var label = 'att1 is ' + att1 + ' and att2 is ' + att2 + ' and att3 is ' + att3 + ' total is ' + product;
        $(this).text(label);
    });
})
</script>


Lines 5-7 are an example of how to read in each value.  The calculation on line 10 and label on line 13 can be updated however you need.
answered Oct 15, 2021 by Zachary Platinum Sawtooth Software, Inc. (208,650 points)
Where do you want the result displayed if not the merged row?
We weren't planning to use a merged row at all, we were hoping to display all the attributes, then a calculated value on each card.
So a new attribute at the top of each concept?
Ideally it would show up at the bottom of each concept but the top would be okay too! We aren't looking to analyze it as a separate attribute though.
Keep the HTML in the CBC levels with the values you want used in the calculation.

Then add a conditional relationship to the CBC with its position set to the bottom of the concept.  The attribute label can include whatever label you want for this row, but also needs some HTML at the end like this:

Score <input type="hidden" class="scoreAttribute"/>


Finally, the JavaScript in the CBC footer should be replaced with this:

<script>
$(document).ready(function(){
    var nthChild = $('.scoreAttribute').closest('.cbc_cell').index() + 1;
    $('.cbc_concept:not(.none_concept)').each(function(){
        // Read levels
        var att1 = Number($(this).find('.att1').val());
        var att2 = Number($(this).find('.att2').val());
        var att3 = Number($(this).find('.att3').val());
         
        // Calculations
        var product = att1 * att2 * att3;
         
        // Print
        var label = 'att1 is ' + att1 + ' and att2 is ' + att2 + ' and att3 is ' + att3 + ' total is ' + product;
        $(this).find('.cbc_cell:nth-child(' + nthChild + ') .level_text').text(label);
    });
})
</script>
...