Support » Plugin: Calculated Fields Form » Formula for multiple selections

  • Resolved Sportuojantys

    (@needfeed)


    Hello, need some help with CFF formula for multiple selections. Thanks in advance…

    Basic formula for calorie intake:
    Men: (10 × weight in kg) + (6.25 × height in cm) – (5 × age in years) + 5
    Women: (10 × weight in kg) + (6.25 × height in cm) – (5 × age in years) – 161

    And the answer from this formula should be multiplied by the number of you physical activity:
    x 1.2: If you are sedentary = little to no exercise in a day
    x 1.375: If you are slightly active = light exercise/sports 2-3 days/week
    x 1.55: If you are moderately active = moderate exercise/sports 4-5 days/week
    x 1.725: If you are very active = hard exercise/sports 6-7 days a week
    x 1.9: If you are extra active = very hard exercise/sports and physical job OR 2x training

    I know how to create formula with 2 physical activity selection (x1.2 and x1.375) formula is below, but how about adding more selections(x1.55; x1.725; x1.9)? I know that it‘s an easy thing for most IT guys :))

    (function(){
    if(fieldname9 == ‘laikyti’) return ROUND(fieldname3==’Man’?((fieldname4==’Little activity’?(((10*fieldname2)+(6.25*fieldname10)-(5*fieldname12)+5)*1.2):(((10*fieldname2)+(6.25*fieldname10)-(5*fieldname12)+5)*1.375))):((fieldname4==’Little activity’?((10*fieldname2)+(6.25*fieldname10)-5*fieldname12)-161*1.2):(((10*fieldname2)+(6.25*fieldname10)-(5*fieldname12)-161)*1.375))));
    })()

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

    In equations with multiple conditions, it is recommended to use switch conditional statements. The ternary operator can be hard to read or edit. The equation could be implemented as follows:

    (function(){
        var result = 10*fieldname2+6.25*fieldname10-5*fieldname12+(fieldname3 == 'Man' ? 5 : -161),
            factor = 1;
    
        switch(fieldname4)
        {
           case 'sedentary ': factor =  1.2; break;
           case 'slightly active': factor =  1.375; break;
           case 'moderately active': factor =  1.55; break;
           case 'very active': factor =  1.725; break;
           case 'extra active': factor =  1.9; break;
        }
    
        return result*factor;
    })()

    Best regards.

    Thread Starter Sportuojantys

    (@needfeed)

    Thank you! 🙂

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.