Support » Plugin: Calculated Fields Form » I need your help with this javscript calculation

• Resolved Horlayemi

(@horlaes)

How do I get this to work?
(function(){
if( fieldname16 == 0 && fieldname20 == 0) return fieldname34;

if( fieldname34 == 0 && fieldname20 == 0) return fieldname16;

if( fieldname34 == 0 && fieldname16 == 0) return fieldname20;

if( fieldname16 >= 1 && fieldname20 >= 1) return fieldname16*fieldname20;

if( fieldname34 >= 1 && fieldname20 >= 1) return fieldname34*fieldname20;

if( fieldname34 >=1 && fieldname16 >= 1) return fieldname34*fieldname16;

*************** Having issue with this part*****************
if( fieldname16 >0 && fieldname20 >0 && fieldname34 >0) return fieldname16*fieldname20*fieldname34;
***************************************************************

})()

Viewing 3 replies - 1 through 3 (of 3 total)
• Plugin Author codepeople

(@codepeople)

Hello @horlaes,

The issue is with the order of conditional statements in the equation, you should from the most specific to the most general, because if the order is incorrect, this conditional statement:

``````
if( fieldname16 >= 1 && fieldname20 >= 1) return fieldname16*fieldname20;
``````

will be satisfied, before the conditional statement:

``````
if( fieldname16 >0 && fieldname20 >0 && fieldname34 >0) return fieldname16*fieldname20*fieldname34;
``````

and the equation will return reach this piece of code.

An alternative would be modify the equation as follows:

``````
(function () {
if (fieldname16 == 0 && fieldname20 == 0)
return fieldname34;
if (fieldname34 == 0 && fieldname20 == 0)
return fieldname16;
if (fieldname34 == 0 && fieldname16 == 0)
return fieldname20;
if (fieldname16 >= 1 && fieldname20 >= 1 && fieldname34 < 1)
return fieldname16 * fieldname20;
if (fieldname34 >= 1 && fieldname20 >= 1 && fieldname16 < 1)
return fieldname34 * fieldname20;
if (fieldname34 >= 1 && fieldname16 >= 1 && fieldname20 < 1)
return fieldname34 * fieldname16;
if (fieldname16 > 0 && fieldname20 > 0 && fieldname34 > 0)
return fieldname16 * fieldname20 * fieldname34;
})()
``````

Best regards.

(@horlaes)

Voila! solved in a minute, thanks a lot codepeople

Plugin Author codepeople

(@codepeople)

It has been a pleasure.

Best regards.

Viewing 3 replies - 1 through 3 (of 3 total)
• The topic ‘I need your help with this javscript calculation’ is closed to new replies.