Skip to main content
Participant
January 20, 2017
Answered

Is there a way to make a field calculate BMI using a formula that would include two fields?

  • January 20, 2017
  • 2 replies
  • 2442 views

I would like a field to automatically calculate BMI when a user enters their height and weight.  (height in inches and weight in pounds are each their own field).  The formula for BMI is:

BMI= (weight in pounds *703)/height in inches squared

Any help or direction would be extremely helpful.

Thank you so much!

This topic has been closed for replies.
Correct answer George_Johnson

You can use a custom calculation script like the following:

// Custom calculation script

(function () {

    // Get the field values, as numbers

    var weight = +getField("WEIGHT").value;

    var height = +getField("HEIGHT").value;

    // Perform the calculation if both values are greater than zero

    if (weight > 0 && height > 0) {

        event.value = 703 * weight / Math.pow(height, 2);

    } else {

        event.value = "";  // Blank this field

    }

})();

Replace "WEIGHT" and "HEIGHT" in the getField statements with the actual names of those fields.

2 replies

Participant
January 20, 2017

Thank you so much!  Is there away to modify the script to reflect that the height is represented by two fields Feet and Inches in which it would multiply the feet by 12 and then add the inches to give a total sum for height?

THANK YOU!!!

try67
Community Expert
Community Expert
January 20, 2017

Yes. Change this line:

var height = +getField("HEIGHT").value;

To:

var height = (+getField("HEIGHT_FEET").value)*12 + (+getField("HEIGHT_INCHES").value);

Participant
January 26, 2017

Thank you for your reply.  I actually ended up doing something similar and it seemed to work:

(function () {

    // Get the field values, as text

    var weight = +getField("E7. a. 2. Weight").value;

    var feet = +getField("E7. a. 1. Height").value;

    var inches = +getField("E7. a. 1. Feet").value;

    var height = (feet * 12) + inches;

    // Perform the calculation if both values are greater than zero

    if (weight > 0 && height > 0) {

        event.value = 703 * weight / Math.pow(height, 2);

    } else {

        event.value = "";  // Blank this field

    }

})();

George_JohnsonCorrect answer
Inspiring
January 20, 2017

You can use a custom calculation script like the following:

// Custom calculation script

(function () {

    // Get the field values, as numbers

    var weight = +getField("WEIGHT").value;

    var height = +getField("HEIGHT").value;

    // Perform the calculation if both values are greater than zero

    if (weight > 0 && height > 0) {

        event.value = 703 * weight / Math.pow(height, 2);

    } else {

        event.value = "";  // Blank this field

    }

})();

Replace "WEIGHT" and "HEIGHT" in the getField statements with the actual names of those fields.