Javascript for Body Mass Index calc? W for weight; H for Height; BMI in 3rd cell. Format, validate, calc settings?

What is the "Custom calculation script" for calculating Body Mass Index, when I have 3 Text Fields -- 1st has H (for height in inches), 2nd has W (for weight in pounds), and 3rd is named BMI. Also, what are entries for Format / Validate for H and W. The formula that works in MS Word 2010 to calculate BMI [which references cell numbers like excel] is:
=703*D3/D2^2
In that case, D3 is the W and D2 is the H squared.
Thanks for your help.


Laurie StOnge


3 Answers

Voted Best Answer

You have to decide the format for the entries. Is height in feet and inches or just inches. If in feet and inches and one field you would need a custom field and custom format and then for the calculation you would need to convert the feet and inches to what unit you need in your calculation. If in inches, a number format with no decimal positions should work. Weight could be just lbs, again a number with no decimal places.

You then need to convert the cell names to field names and convert the formula to JavaScript. Since the multiplication and division symbols are the same that should cause no problem. Exponentiation is done through the Math objects pow properry.

Assuming your height field in inches is a field named "Height" and the weight in pounds is in field "Mass"

For the BMI calculation you could use the following custom JavaScript calculation:

// get the values of the fields
var mass = this.getField("Mass").value;
var height = this.getField("Height").value;
// set value if missing values
event.value = "";
// perform calculation if we have valid data
if(mass != "" && height != "") {
// square the height
var height2 = Math.pow(height, 2);
// compute the division
event.value = mass /height2
// aplly conversion to engilish units
event.value = event.value * 703.06957964
}


By George Kaiser   

If you want to use a function and be able specify the unit of measurements:

// document level funciotn
function BMI (nMass, nHeight, cUnits) {
// set default units for calculation - English or Metric
if(typeof cUnits == "undefined") cUnits = "English";
// mass pounds, height inches
var nBMI = 0;
// perform calculation if we have valid data
if(nMass != "" && nHeight != "") {
// square the height
var nHeight2 = Math.pow(nHeight, 2);
// compute the division
nBMI = nMass /nHeight2
switch (cUnits.toLowerCase()) {
case "english":
// aplly conversion to engilish units
nBMI = nBMI * 703.06957964;
break;
case "metric":
// no adjustment needed
break;
default:
nBMI = 0;
break;
} // end switch units
} // end if data not null
// return computed BMI
return nBMI;
} // end BMI funciton

Custom JavaScript for BMI field:

// name for fields
var cMass = "Mass";
var cHeight = "Height";
// calculaiton assumes height is in inches
// calculaiton assumes mass is in pounds
// get the values of the fields
var mass = this.getField("Mass").value;
var height = this.getField("Height").value;
// get the values of the fields
var mass = this.getField("Mass").value;
var height = this.getField("Height").value;
event.value = BMI(mass, height, "English");

You can remove the "English" parameter if you want to. If you change "English" to "Metric" the computation will use metric units of the height in meters and weight in kilograms.


George Kaiser   

Thank you, George Kaiser! Fabulous, just fabulous!! Wow! It works!!!!! Whoo hooooo!!!!! Merry Christmas!!


Laurie StOnge   


Please specify a reason: