• Resolved Trucker

    (@trucker)


    Hi, I am trying to create a complex function like this using Calculated Fields Form:

    User submits a number to fieldname 1 and fieldname 2.

    For example, the total cost of level 2 to level 4 would be 200+500+500

    (function(){
    var startLevel = fieldname1, endLevel = fieldname2;
    
    var a = [
    {level: 1, fee: 0},
    {level: 2, fee: 200},
    {level: 3, fee: 500},
    {level: 4, fee: 500},
    {level: 5, fee: 500},
    ]
    
    var totalFee = 0
    for(i=startLevel; i<endLevel; i++){
        totalFee += a[i]?.fee
    }
    return totalFee;
    })();

    However, whenever I Preview the code, it doesn’t show the final value totalFee on the Calculated Field.

    Thank you a lot!

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

    (@codepeople)

    Hello @trucker

    First, in array the index in the array begins in zero. So, if the user enter a number between 1 and 5 in the fieldname1 and fieldname2 fields, you should subtract 1.

    Second, the plugin minifies the equations’ codes, so, the use of semicolons at the end of the lines of code are no optionals.

    Third, the use of “?” symbol in your code is incorrect.

    The correct equation code would be:

    
    (function(){
    var startLevel = fieldname1-1, endLevel = fieldname2-1,
    a = [
    {level: 1, fee: 0},
    {level: 2, fee: 200},
    {level: 3, fee: 500},
    {level: 4, fee: 500},
    {level: 5, fee: 500},
    ],
    totalFee = 0;
    
    for(var i=startLevel; i<=endLevel; i++){
        totalFee += a[i].fee;
    }
    return totalFee;
    })();
    

    Best regards.

Viewing 1 replies (of 1 total)
  • The topic ‘Calculate the Sum of an Array Cost’ is closed to new replies.