Support » Plugin: Calculated Fields Form » Conditional If equation for multiple choices

  • Resolved yourlaks

    (@yourlaks)


    My calculator is based on activity levels (Sedentary, Light, Moderate, Very, Active). How do I get one equation to work?

    My code:

    (function(){
    var result = IF (fieldname7 == ‘Sedentary’, PREC (fieldname14 * 1.2, 1)) ; (fieldname7 == ‘Light’, PREC (fieldname14 * 1.4, 1)) ; (fieldname7 == ‘Moderate’, PREC (fieldname14 * 1.6, 1)) ; (fieldname7 == ‘Very’, PREC (fieldname14 * 1.75, 1)) ; (fieldname7 == ‘Extra’, PREC (fieldname14 * 2, 1)) ; (fieldname7 == ‘Active’, PREC (fieldname14 * 2.3, 1));
    jQuery(‘.tci-result-here’).html(CONCATENATE(‘activity levels : ‘, result));
    return result;
    })()

    • This topic was modified 2 years, 6 months ago by yourlaks.
    • This topic was modified 2 years, 6 months ago by yourlaks.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author codepeople

    (@codepeople)

    Hello @yourlaks

    The separator between the operands in the operations is the comma symbol and not the semicolon. Furthermore, there are other issues:

    The “IF” operation requires three parameters and not two. Your use of the parenthesis is incorrect, and other minor errors.

    Please, remember you can nest multiple “IF” operations.

    Your equation can be implemented as follows:

    (function(){
    var result = PREC(fieldname14*IF(fieldname7 == 'Sedentary', 1.2, IF(fieldname7 == 'Light', 1.4, IF(fieldname7 == 'Moderate', 1.6, IF(fieldname7 == 'Very', 1.75, IF(fieldname7 == 'Extra', 2, 2.3))))), 1);
    
    jQuery('.tci-result-here').html(CONCATENATE('activity levels : ', result));
    return result;
    })()

    However, there are other more preferred implementations because they are more readable, using the “if” conditional statement (do not confuse it with the “IF” operation), or with the “switch” conditional statement.

    Using “if” conditional statement:

    (function(){
    var result = fieldname14, factor = 1;
    
    if(fieldname7 == 'Sedentary') factor = 1.2;
    if(fieldname7 == 'Light') factor =1.4;
    if(fieldname7 == 'Moderate') factor = 1.6;
    if(fieldname7 == 'Very') factor = 1.75;
    if(fieldname7 == 'Extra') factor = 2;
    if(fieldname7 == 'Active') factor = 2.3;
    
    result = PREC(result*factor, 1);
    
    jQuery('.tci-result-here').html(CONCATENATE('activity levels : ', result));
    return result;
    })()

    Using “switch” conditional statement:

    (function(){
    var result = fieldname14, factor = 1;
    
    switch(fieldname7)
    {
        case 'Sedentary': factor = 1.2; break;
        case 'Light': factor =1.4; break;
        case 'Moderate': factor = 1.6; break;
        case 'Very': factor = 1.75; break;
        case 'Extra': factor = 2; break;
        case 'Active': factor = 2.3; break;
    }
    
    result = PREC(result*factor, 1);
    
    jQuery('.tci-result-here').html(CONCATENATE('activity levels : ', result));
    return result;
    })()

    Best regards.

    Thread Starter yourlaks

    (@yourlaks)

    Thanks for being exceptionally quick!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Conditional If equation for multiple choices’ is closed to new replies.