• Resolved fazilaadam

    (@fazilaadam)


    Hi
    I am new to javascript and the cff plugin. I need to write a function in a calculation that uses 5 options from a drop-down list to calculate 5 possible results. I have tried the following, but it always calculates only the first option regardless of which drop down is selected. Please can you help correct the equation?

    (function () {
    var result=0; reference = fieldname31;
    if (reference =’Sedentary’) result += fieldname4*1.2;
    else if (reference =’Mild’) result += fieldname4*1.3-1.375;
    else if (reference =’Moderate’) result += fieldname4*1.5-1.55;
    else if (reference =’Heavy’) result += fieldname4*1.7;
    else if (reference =’Extreme’) result += fieldname4*1.9;

    return result;
    })();

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

    (@codepeople)

    Hello @fazilaadam,

    The issue is simple, in javascript the equality operator is represented with the double sign: “==” because the sign “=” is used for assignment, furthermore, if you want declare multiple javascript variables in a same var statement, you should separate them with comma symbol (,) and not semicolon, so, the equation would be:

    
    (function () {
    var result=0, reference = fieldname31;
    if (reference =='Sedentary') result += fieldname4*1.2;
    else if (reference =='Mild') result += fieldname4*1.3-1.375;
    else if (reference =='Moderate') result += fieldname4*1.5-1.55;
    else if (reference =='Heavy') result += fieldname4*1.7;
    else if (reference =='Extreme') result += fieldname4*1.9;
    return result;
    })();
    

    To optimize a little more, you can remove event the last “if” because it is the option remained after checking the other four options:

    
    (function () {
    var result=0, reference = fieldname31;
    if (reference =='Sedentary') result += fieldname4*1.2;
    else if (reference =='Mild') result += fieldname4*1.3-1.375;
    else if (reference =='Moderate') result += fieldname4*1.5-1.55;
    else if (reference =='Heavy') result += fieldname4*1.7;
    else result += fieldname4*1.9;
    return result;
    })();
    

    Finally, there is not an unique way to implement the equation, javascript is a very rich language with many possibilities, an alternative would be the use of the “switch” conditional statement as follows:

    
    (function () {
      switch(fieldname31)
      {
        case 'Sedentary': return fieldname4*1.2;
        case 'Mild': return fieldname4*1.3-1.375;
        case 'Moderate': return fieldname4*1.5-1.55;
        case 'Heavy': return fieldname4*1.7;
        default: return fieldname4*1.9;
      }
    })();
    

    Best regards.

    Thread Starter fazilaadam

    (@fazilaadam)

    Thanks so much! That works perfectly.

    Plugin Author codepeople

    (@codepeople)

    Hello @fazilaadam,

    It has been a pleasure.

    Best regards.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘jscript function to use drop down field in calculation’ is closed to new replies.