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. (216,575 points)
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>
Hey Zachary,

Do you have a suggestion on how to do this if there are too many variables to create a conditional relationship?
Sorry, there's been a lot in this thread so far.  Can you expound on what exactly you need?  Is the original merged row solution viable for you?
Hi Zachary,

I was hoping to show all the attributes, but add on a calculated "total value" attribute at the bottom of each concept. The issue is this CBC has a lot of variables so I can't generate the conditional display. I don't think the merged row is viable as it will hide all the other attributes.

Thank you for getting back to me!
Alright, here's how I would do it.  For the levels of the CBC that contribute to the total, add HTML like this:

<input type="hidden" class="hiddenValue1" value="7"/>


"hiddenValue1" should be used for the first attribute involved in the total, "hiddenValue2" for the next attribute involved, etc.  Of course, the "value" property should be set to whatever value this level ought to contribute to the total.  Like always, I recommend setting an internal label to these levels so this HTML doesn't show up in analysis.

Then add the conditional display row to the bottom of the CBC.  It doesn't matter what attributes are included in it, so I'd just include a single one for simplicity.  Set all the conditional displays to something like this:

<span class="computedTotal"></span>


Feel free to put something like "$" or "per month" around that HTML if you want to display something along with the total here.

Finally, add this script to the CBC:

<script>
$(document).ready(function(){
    $('#[% QuestionName() %]_div .cbc_concept:not(.none_concept)').each(function(){
        var hiddenValue1 = Number($(this).find('.hiddenValue1').val()) || 0;
        var hiddenValue2 = Number($(this).find('.hiddenValue2').val()) || 0;
        var hiddenValue3 = Number($(this).find('.hiddenValue3').val()) || 0;
        var total = hiddenValue1 + hiddenValue2 + hiddenValue3;
        $(this).find('.computedTotal').text(total);
    });
})
</script>


Lines 4-6 can be extended to handle however many attributes you have involved with this total.  Line 7 controls how the total is calculated; for now, I've assumed that it is just the mathematical total of the values set in the level HTML.
...