Expand my Community achievements bar.

SOLVED

Weight, Height, BMI Calculator

Avatar

Level 2

I am working on a simple pdf that converts lbs -> kg, ft'in" -> cm and calculates BMI.  If the user checks lbs then a hidden convertion field becomes visible (the ht. works in the same fashion).

I've been sucessful with my scripts for everything besides the BMI calculation part.  Actually, the script works fine if you just keep clicking OK on the warning box and the calculation works!  Also, the "Check Sript Syntax" doesn't pick up the error... I'm not sure what is wrong with the script to cause the warning dialog box even thought it works fine.

Any help would be appriciated.

Thanks!

if (q1.lbs2kg.Wt_lbs_Input.rawValue > 0 and q2.st2met.Ht_met_Cal.rawValue > 0) then
$.rawValue = q1.lbs2kg.Wt_kg_Cal.rawValue / ((q2.st2met.Ht_met_Cal.rawValue * .01) * (q2.st2met.Ht_met_Cal.rawValue * .01));

elseif (q1.lbs2kg.Wt_kg_Cal.rawValue > 0 and q2.met2st.Ht_met_Input.rawValue > 0) then
$.rawValue = q1.lbs2kg.Wt_kg_Cal.rawValue / ((q2.met2st.Ht_met_Input.rawValue * .01) * (q2.met2st.Ht_met_Input.rawValue * .01));

elseif (q1.kg2lbs.Wt_kg_Input.rawValue > 0 and q2.st2met.Ht_met_Cal.rawValue > 0) then
$.rawValue = q1.kg2lbs.Wt_kg_Input.rawValue / ((q2.st2met.Ht_met_Cal.rawValue *.01) * (q2.st2met.Ht_met_Cal.rawValue *.01));

elseif (q1.kg2lbs.Wt_kg_Input.rawValue > 0 and q2.met2st.Ht_met_Input.rawValue > 0) then
$.rawValue = q1.kg2lbs.Wt_kg_Input.rawValue / ((q2.met2st.Ht_met_Input.rawValue * .01) * (q2.met2st.Ht_met_Input.rawValue * .01));

endif

Also, its worth noting that the formula has to have 4 different conditions based upon how the user inputs the information.

1 Accepted Solution

Avatar

Correct answer by
Former Community Member

You have your expression on the calculate event. That event will fire when the form loads (and at this point there is no value). I would wrap your expression around a condition that checks if these fields are null or not. If they are null then they have not been filled yet and you should not do the calculation.

Paul

View solution in original post

6 Replies

Avatar

Level 2

I would like to add the pdf as an attachment on this thread, but I do not see an attachment button on the forum post?

This is a google doc share link.  You will have to save it and then open it because google docs do not support dynamic pdfs.

https://docs.google.com/viewer?a=v&pid=explorer&chrome=true&srcid=0B8kmksHZdTW3YWY4ZDkwOTEtNmY1NC00Y...

Avatar

Correct answer by
Former Community Member

You have your expression on the calculate event. That event will fire when the form loads (and at this point there is no value). I would wrap your expression around a condition that checks if these fields are null or not. If they are null then they have not been filled yet and you should not do the calculation.

Paul

Avatar

Level 2

I thought I accomplished that in the 'if' part of the script with rawValve.?

if (q1.lbs2kg.Wt_lbs_Input.rawValue > 0 and q2.st2met.Ht_met_Cal.rawValue > 0)

Also, I forgot to mention that this is in FormCalc

Avatar

Former Community Member

If you check the value before you fill it I think you will find that it is null not 0.

Doesn't matter what language it is.

Paul

Avatar

Level 10

I'd try the FormCalc HasValue() function.

if (HasValue(q1.lbs2kg.Wt_lbs_Input) and HasValue(q2.st2met.Ht_met_Cal))

(I think I have the syntax correct)

Oh, and with FormCalc you don't need ".rawValue" or the semicolons.

Avatar

Level 2

Issue Fixed, thanks Paul!

I had to change if (q1.lbs2kg.Wt_lbs_Input.rawValue > 0.. to if (q1._lbs2kg.count >= 1

Also,  I ran into another issue (Arithmetic over/underflow) when the ht. values were expressed first b/c x/0 = unresolved.  I resolved that by placing an if statement within an if statemtent to make sure values in the demoniator had to > 0

form1.#subform[0].BMI::calculate - (FormCalc, client)

form1.#subform[0].BMI::calculate - (FormCalc, client)

if (q1._lbs2kg.count >= 1 and q2._st2met.count >= 1) then

    if (q2.st2met.Ht_st_Input > 1) then

        $.rawValue = ceil(q1.lbs2kg.Wt_kg_Cal / Pow((q2.st2met.Ht_met_Cal * .01),2));

        endif

elseif (q1._lbs2kg.count >= 1 and q2._met2st.count >= 1) then

    if (q2.met2st.Ht_met_Input > 1) then

        $.rawValue = ceil(q1.lbs2kg.Wt_kg_Cal / Pow((q2.met2st.Ht_met_Input * .01),2));

        endif

elseif (q1._kg2lbs.count >= 1 and q2._st2met.count >= 1) then

    if (q2.st2met.Ht_st_Input > 1) then

        $.rawValue = ceil(q1.kg2lbs.Wt_kg_Input / Pow((q2.st2met.Ht_met_Cal * .01),2));

        endif

elseif (q1._kg2lbs.count >= 1 and q2._met2st.count >= 1) then

    if (q2.met2st.Ht_met_Input > 1) then

        $.rawValue = ceil(q1.kg2lbs.Wt_kg_Input / Pow((q2.met2st.Ht_met_Input * .01),2));

        endif

endif

Hope this helps others as well!

https://docs.google.com/viewer?a=v&pid=explorer&chrome=true&srcid=0B8kmksHZdTW3N2M2N2Q1MjgtMjAxYi00Z...

The following has evaluated to null or missing: ==> liqladmin("SELECT id, value FROM metrics WHERE id = 'net_accepted_solutions' and user.id = '${acceptedAnswer.author.id}'").data.items [in template "analytics-container" at line 83, column 41] ---- Tip: It's the step after the last dot that caused this error, not those before it. ---- Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: #assign answerAuthorNetSolutions = li... [in template "analytics-container" at line 83, column 5] ----