Support » Plugin: Calculated Fields Form » Using Multiple If Else

  • Resolved spiritxanas

    (@spiritxanas)


    Hey so what I’m trying to do is a little bit complex hope you can help me out
    So I have 7 field boxes, each with a grade A B C D E S or U which a user can select through a dropdown. So each of the 7 fieldnames have 7 Grade options a user can select which will then be calculated as a fixed Rank Grade in the final sum as I need to add the points of all 7 fieldboxes into a final calculated box.
    Now I did think of creating a function, but that would only be useful for a single field box with multiple returns
    (function(){
    if(fieldname15 == ‘A’) return 20;
    if(fieldname15 == ‘B’) return 17.5;
    if(fieldname15 == ‘C’) return 15;
    if(fieldname15 == ‘D’) return 12.5;
    if(fieldname15 == ‘E’) return 10;
    if(fieldname15 == ‘S’) return 5;
    if(fieldname15 == ‘U’) return 0;
    return -1;
    })();`

    So the only option I have is to create 7 IF statements for each of the 7 fields which would total to 49 lines of code, as shown above
    I was hoping there would be an efficient way to tackle this? Like maybe a parameter for the function so I could just send a fieldname into a function and get a grade/mark out

    Also how do you assign the value of a function return to a fielddbox? The documentation has missed that part out`

Viewing 1 replies (of 1 total)
  • Plugin Author codepeople

    (@codepeople)

    Hello @spiritxanas

    The easiest solution would be to assign the values directly to the fields’ choices. In the Dropdown fields, the choices have assigned a text and value. You can enter, for example, the letter A as the choice’s text and the number 20 as its value. So, assuming the dropdown fields are fieldname15, fieldname16, fieldname17, fieldname18, fieldname19, fieldname20, and fieldname21, the equation would be:

    SUM(fieldname15, fieldname16, fieldname17, fieldname18, fieldname19, fieldname20, fieldname21)

    But if you need the letters as the choices’ values too because you use them for other reasons, you can implement the equation with a loop statement:

    
    (function(){
        var fields = [fieldname15, fieldname16, fieldname17, fieldname18, fieldname19, fieldname20, fieldname21], result = 0;
    
        for(var i in fields)
        {
            switch(fields[i])
            {
                case 'A': result += 20; break;
                case 'B': result += 17.5; break;
                case 'C': result += 15; break;
                case 'D': result += 12.5; break;
                case 'E': result += 10; break;
                case 'S': result += 5; break;
                case 'U': result += 0; break;
                default: result += -1; break;
            }
        }
        return result;
    })()
    

    Best regards.

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