Summary

This article covers a script example of BMI Calculation.

Table of Contents


Description

This script is written to calculate the bmi based on the weight and height entered. The script first checks to make sure that both weight and height are not blank. It then evaluates the unit question. If the “US” unit is selected then it uses the bmi command to assign the value to the bmi question based on the US formula. If the “US” unit is not selected then it uses the bmi command to assign the value to the bmi question based on the SI formula. The targets are the height, weight and unit questions so that the bmi will recalculate if either height and weight are changed.

Note: The height for the SI formula is read in meters. If data is provided in centimeters, the height must be divided by 100 before applying the SI formula.


Script

<script scriptId="bmiCalculation" >
    <body>
        <s:if>
            <s:and>
                <s:isSet>                       
                         <s:question>
                            <s:question path=".weight"/>
                         </s:question>                           
                </s:isSet>
                <s:isSet>                       
                         <s:question>
                            <s:question path=".height"/>
                         </s:question>                           
                </s:isSet>
                <s:gt>
                    <s:numberPath path=".weight:value"/>
                    <s:number value="0"/>
                </s:gt>
                <s:gt>
                    <s:numberPath path=".height:value"/>
                    <s:number value="0"/>
                </s:gt>
            </s:and>
        <s:assign>
                <s:question path=".bmi" />
                    <s:round decimals="2">
                        <s:bmi>
                        <s:weight>
                            <s:numberPath path=".weight:value"/>
                        </s:weight>
                        <s:height>
                            <s:numberPath path=".height:value"/>
                        </s:height>
                        <s:formula>
                            <s:string value="US" />
                        </s:formula>
                    </s:bmi>
                </s:round>
            </s:assign>
            <s:assign>
                <s:question path=".bmi" />
                <s:string value=""/>
            </s:assign>
        </s:if>
        </body>
    <target action="update" typeId="height" when="preprocess" shouldReconcile='true' />
    <target action="update" typeId="weight" when="preprocess" shouldReconcile='true' />
</script>



Need more help?

Please visit the Fountayn Contact Information page.