Title: Function for complex operation not working. Please help :)
Last modified: July 1, 2017

---

# Function for complex operation not working. Please help :)

 *  Resolved [digiblogger](https://wordpress.org/support/users/digiblogger/)
 * (@digiblogger)
 * [8 years, 11 months ago](https://wordpress.org/support/topic/function-for-complex-operation-not-working-please-help/)
 * Hi there,
 * I try to implement and logical operation in an calculated field like this:
 *     ```
       (function(){
       if(fieldname3 = 0) return prec((fieldname16/fieldname15*fieldname4)+(fieldname20/fieldname19*fieldname11),2);
       if(fieldname3 > 0) return prec((fieldname16/fieldname15*fieldname4)+(fieldname18/fieldname17*fieldname10)+(fieldname20/fieldname19*fieldname11),2);
       })();
       ```
   
 * This does not work. The basic calculation works:
 *     ```
       prec((fieldname16/fieldname15*fieldname4)+(fieldname18/fieldname17*fieldname10)+(fieldname20/fieldname19*fieldname11),2)
       ```
   
 * But only if fieldname3 is not 0
 * For better understanding.
    If fieldname3 is 0, the user will enter 0 in fieldname17
   too. this will cause an operation “divided by zero”, which is not defined. So
   I need this if rule to be sure, if fieldname3 is 0, the calculation will be made
   without the second bracket, which is:
 * `(fieldname18/fieldname17*fieldname10)+`
    -  This topic was modified 8 years, 11 months ago by [digiblogger](https://wordpress.org/support/users/digiblogger/).

Viewing 5 replies - 1 through 5 (of 5 total)

 *  Plugin Author [codepeople](https://wordpress.org/support/users/codepeople/)
 * (@codepeople)
 * [8 years, 11 months ago](https://wordpress.org/support/topic/function-for-complex-operation-not-working-please-help/#post-9280556)
 * Hello [@digiblogger](https://wordpress.org/support/users/digiblogger/),
 * The issue is simple, in javascript the symbol “=” is used for assignment, the
   comparison operator is the double symbol: “==”, so the equation should be modified
   as follows:
 *     ```
       (function(){
       if(fieldname3 == 0) return PREC((fieldname16/fieldname15*fieldname4)+(fieldname20/fieldname19*fieldname11),2);
       if(fieldname3 > 0) return PREC((fieldname16/fieldname15*fieldname4)+(fieldname18/fieldname17*fieldname10)+(fieldname20/fieldname19*fieldname11),2);
       })();
       ```
   
 * There are other alternatives to implement the same equation, for example, using
   the “IF” operation:
 * `PREC(IF(fieldname3 == 0, fieldname16/fieldname15*fieldname4+fieldname20/fieldname19*
   fieldname11, fieldname18/fieldname17*fieldname10+fieldname20/fieldname19*fieldname11),
   2)`
 * Best regards.
 *  Thread Starter [digiblogger](https://wordpress.org/support/users/digiblogger/)
 * (@digiblogger)
 * [8 years, 11 months ago](https://wordpress.org/support/topic/function-for-complex-operation-not-working-please-help/#post-9282249)
 * Great this works. Thank you.
 * Is there also a reason to have a logical OR and AND operator?
 * For example “if field2 or field3 = 0 then…”
 *  Plugin Author [codepeople](https://wordpress.org/support/users/codepeople/)
 * (@codepeople)
 * [8 years, 11 months ago](https://wordpress.org/support/topic/function-for-complex-operation-not-working-please-help/#post-9282314)
 * Hello,
 * In javascript the “or” operator is represented with the double symbol: “||” and
   the “and” operator: “&&”
 * For example, if you want check if the fields: fieldname1 and fieldname2 are different
   to zero:
 *     ```
       (function(){
       if(fieldname1 != 0 && fieldname2 != 0) return fieldname1+fieldname2;
       return '';
       })()
       ```
   
 * More information in the following links:
 * [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators)
 * [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if…else](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if…else)
 * Best regards.
 *  Thread Starter [digiblogger](https://wordpress.org/support/users/digiblogger/)
 * (@digiblogger)
 * [8 years, 10 months ago](https://wordpress.org/support/topic/function-for-complex-operation-not-working-please-help/#post-9301692)
 * Okay.. So i made this expression:
 *     ```
       (function(){
       if(fieldname6 = 0 || fieldname17 = 0 || fieldname18 = 0) return PREC((fieldname16/fieldname15*fieldname4)+(fieldname20/fieldname19*fieldname11),2);
       if(fieldname7 = 0 || fieldname19 = 0 || fieldname20 = 0) return PREC((fieldname16/fieldname15*fieldname4)+(fieldname18/fieldname17*fieldname10),2);
       PREC((fieldname16/fieldname15*fieldname4)+(fieldname18/fieldname17*fieldname10)+(fieldname20/fieldname19*fieldname11),2);
       })()
       ```
   
 * But it does not work.
    I try in words what I want to achieve:
 * If one of the fields 6,17,18 is 0 then
    PREC((fieldname16/fieldname15*fieldname4)
   +(fieldname20/fieldname19*fieldname11),2); If one of the fields 7,19,20 is 0 
   then PREC((fieldname16/fieldname15*fieldname4)+(fieldname18/fieldname17*fieldname10),
   2); Else PREC((fieldname16/fieldname15*fieldname4)+(fieldname18/fieldname17*fieldname10)
   +(fieldname20/fieldname19*fieldname11),2);
 * I checked my code a dozen times but don´t find the mistake
 *  Plugin Author [codepeople](https://wordpress.org/support/users/codepeople/)
 * (@codepeople)
 * [8 years, 10 months ago](https://wordpress.org/support/topic/function-for-complex-operation-not-working-please-help/#post-9301793)
 * Hello [@digiblogger](https://wordpress.org/support/users/digiblogger/),
 * The issue is very simple, the programming language used by browsers is javascript(
   the same language used to implement the equations), and in javascript the comparison
   operator for equality is the double symbol: “==”, because the symbol: “=” is 
   used for assignment.
 * So, the equation would be:
 *     ```
       (function(){
       if(fieldname6 == 0 || fieldname17 == 0 || fieldname18 == 0) return PREC(fieldname16/fieldname15*fieldname4+fieldname20/fieldname19*fieldname11,2);
       if(fieldname7 == 0 || fieldname19 == 0 || fieldname20 == 0) return PREC(fieldname16/fieldname15*fieldname4+fieldname18/fieldname17*fieldname10,2);
   
       return PREC(fieldname16/fieldname15*fieldname4+fieldname18/fieldname17*fieldname10+fieldname20/fieldname19*fieldname11,2);
       })()
       ```
   
 * Best regards.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Function for complex operation not working. Please help :)’ is closed
to new replies.

 * ![](https://ps.w.org/calculated-fields-form/assets/icon-256x256.jpg?rev=1734377)
 * [Calculated Fields Form](https://wordpress.org/plugins/calculated-fields-form/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/calculated-fields-form/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/calculated-fields-form/)
 * [Active Topics](https://wordpress.org/support/plugin/calculated-fields-form/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/calculated-fields-form/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/calculated-fields-form/reviews/)

## Tags

 * [calculation](https://wordpress.org/support/topic-tag/calculation/)
 * [complex](https://wordpress.org/support/topic-tag/complex/)

 * 5 replies
 * 2 participants
 * Last reply from: [codepeople](https://wordpress.org/support/users/codepeople/)
 * Last activity: [8 years, 10 months ago](https://wordpress.org/support/topic/function-for-complex-operation-not-working-please-help/#post-9301793)
 * Status: resolved