• Resolved simonadolfsson

    (@simonadolfsson)


    Hi everyone. Despite being new to the world of creation of websites and wordpress, I have a strong urge to learn more. Right now I am building a website that helps people that are paying to much in rent (in Sweden) to get this money back (100% for free).

    Right now I am using CFF to make a short computation for estimating how much money can be vindicated.

    There are a few numerical values added, for which the “normal” calculated fields gives me no problems. But I would like an IF-value derived from a dropdown meny (Nej, Delvis, Ja = No, Partially, Yes) for making an addition to the computated return of the numerical forms with a percentage.

    Here is the code without the IF statement.

    (fieldname2*fieldname6)-(fieldname10*fieldname6)

    Where
    fieldname2 = any number
    fieldname6 = number between 1-12
    fieldname10 = any number.

    This gives me no problems.

    Here is the code in which I have added an IF-statement. I am making a misstake here, but my knowledge of JS is still to low to see the problem. The fieldname11 is a dropdownlist of (Nej, Delvis, Ja).

    (function(){

    if(fieldname11 = ‘Nej’) return
    ((fieldname2*fieldname6)-(fieldname10*fieldname6));

    if(fieldname11 = ‘Delvis’) return
    ((fieldname2*fieldname6)-((fieldname10*fieldname6)*1,05));

    if(fieldname11 = ‘Ja’) return
    ((fieldname2*fieldname6)-((fieldname10*fieldname6)*1,1));

    })();

    As you see I would like to multiply the calculated field with 5% if ‘Delvis’, and with 10% if “Ja”.

    Where is the problem?

    The page I need help with: [log in to see the link]

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

    (@codepeople)

    Hello @simonadolfsson,

    The issue is very simply, in javascript the comparison operator for equality is the double symbol “==”, because the single symbol “=” is used for assignment, so, the correct structure of the equation would be:

    (function(){
    if(fieldname11 == 'Nej') return ((fieldname2*fieldname6)-(fieldname10*fieldname6));
    
    if(fieldname11 == 'Delvis') return ((fieldname2*fieldname6)-((fieldname10*fieldname6)*1.05));
    
    if(fieldname11 == 'Ja') return ((fieldname2*fieldname6)-((fieldname10*fieldname6)*1.1));
    
    })();

    Note too that the symbol for decimal separator is the dot and not the comma.

    By the way there are better structures to represent your equation, furthermore there are to many unnecessary parenthesis, an alternative would be:

    (function(){
      switch(fieldname11)
      {
        case 'Nej': return fieldname2*fieldname6-fieldname10*fieldname6;
        case 'Delvis': return fieldname2*fieldname6-fieldname10*fieldname6*1.05;
        case 'Ja': return fieldname2*fieldname6-fieldname10*fieldname6*1.1;
      }
    })();

    Best regards.

    Thread Starter simonadolfsson

    (@simonadolfsson)

    @codepeople

    Thank you loads! Have a very great day.

    Simon

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Troubles with IF statements from dropdown meny’ is closed to new replies.