• Resolved digiblogger

    (@digiblogger)


    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 7 years, 8 months ago by digiblogger.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author codepeople

    (@codepeople)

    Hello @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

    (@digiblogger)

    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

    (@codepeople)

    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/Statements/if…else

    Best regards.

    Thread Starter digiblogger

    (@digiblogger)

    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

    (@codepeople)

    Hello @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.