Proper solution
NOTE: I'm using names WeightInput
and HeightInput
for my inputs, Their type is Text input. If your configuration is different, you might amend your formula to get value with proper type.
NOTE: To make the process clear, I'll use separate label element for each step. Then I'll be accessing the value of previous step by the name of the label.
Step 1: Get weight as number
The formula WeightInput.Text
returns text value. We need to use Value()
function to convert it to number:
Value(WeightInput.Text)
I save it to the label named WeightData
.
Step 2: Get squared value of height
We already know how to get number. Let's repeat the same with the addition of Power()
function (exponent value will be 2 in that case):
Power(Value(HeightInput.Text),2)
I save it to label named SquaredWeight
Step 3: Calculate BMI
Now we can use labels we saved and calculate BMI value:
703*(WeightData/SquaredWeight)
I save it as BMI.
Step 4: Return text value
Now, let's create another label which will contain the text value:
If(
Value(BMI.Text) > 30,
"Obese",
Value(BMI.Text) >= 24.5,
"Overweight",
Value(BMI.Text) > 18.5,
"Normal",
Value(BMI.Text) < 18.5,
"underweight"
)
Be careful with the order of conditions. If you do it the other way, you'll receive Normal for all values bigger than 18.5.
This is because in If
function, first condition evaluated to true stops the execution of the function.
Why the existing solution doesn't work
I can see few issues with the existing code. Most of them can be easily noticed if you follow the steps above, but I think it might be nice to point them out:
- No conversion from text to number
- Incorrect use of multi-condition
If
- Mixing the number value with the descriptive value
To show what I mean by mixing the values. Here you save descriptive value to variable:
Set(VarResult,If(tx1.Text/(tx2.Text^2)*703)=<18.5,"underweight")
And later you try to compare it with number value:
if(VarResult>18.5,"Normal")