• Resolved wilfried1954

    (@wilfried1954)


    Hello,

    I need to calculate consumer index based on start and end date (only month and year of course).

    So I think the most friendly option (for lists of 20 year) is to have 2 dropdown lists: month and year. The combination of those fields is the value to calculate.

    What do you think is the best choice to code the values? I think on a (very long) case statement, but maybe other possibilities? How to do this?

    I started with the free version. I do not know if I have this possibility with the payed version.

    Thank you, Wilfried

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

    (@codepeople)

    Hello @wilfried1954,

    The equation to implement depend on project, however, if you want get the number of days between two dates, the easier way would be insert two date fields in the form (for example, fieldname1 and fieldname2) and calculate the difference, for example:

    
    (function(){
    var days = ABS(fieldname1-fieldname2), result;
    
    /** your code to assign the value to the result variable here **/
    return result;
    })()
    

    If you prefer emulate the process with dropdown fields, assuming there are two pairs of fields for the months and years:

    Year 1: fieldname1
    Month 1: fieldname2

    Year 2: fieldname3
    Month 2: fieldname4

    The equation would be:

    
    (function(){
    var d1 = new Date(fieldname1, fieldname2-1, 1), d2 = new Date(fieldname3, fieldname4-1, 1), days, result;
    
    days = FLOOR(ABS(d1-d2)/86400000);
    
    /** your code to assign the value to the result variable here **/
    return result;
    })()
    

    Best regards.

    Thread Starter wilfried1954

    (@wilfried1954)

    Hi,

    Thank you for fast answer. However I did not explain myself very good, English is not my native language. I try again.

    I need a list of every month in the last 20 year. Every month has a certain value. I have to make calculations with that value.

    I could make a dropdown list but this is not user friendly: user should choose an item in a dropdown list with 20 * 12 = 240 items. So better I make 2 lists: 1 for the months and one for the years.

    My first idea is to make a long case statement, something like this:

    switch (year) {
       case 2000:
          switch (month) {
            case 1:
               return 101.01;
            case 2:
               return 105.22;
            // and so on for 12 months of 2000
       case 2001:
          switch (month) {
            case 1:
               return 108.11;
            // and so on for 12 months of 2001
       case 2002:
    // and so on for every month in 20 year

    So every combination of month + year gives a certain value.

    My 2 questions are:
    1. This will be a very long case statement. Is there a better way?
    2. Where do I place the code? This add-on is very new to me.

    • This reply was modified 6 years, 9 months ago by wilfried1954.
    • This reply was modified 6 years, 9 months ago by wilfried1954.
    Thread Starter wilfried1954

    (@wilfried1954)

    Hi,

    I think I have found a proper solution with a multidimensional array. I share it of course. This should work very fast because it need only index pointer in array.

    I placed a HTML content field on the form with global array in it:

    <script>
    var index = [
      [10, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
      [30.4, 40, 50, 60, 70, 80, 90, 100, 200, 333, 222, 111],
      // etcetera
    ];
    </script>

    Then the result code to get the right number depending on the dropdown values:
    index[fieldname2][fieldname3]

    Best regards, Wilfried

    Plugin Author codepeople

    (@codepeople)

    Hello @wilfried1954,

    Yes, the use of a multidimensional array is the perfect solution in this case. Thank you very much for sharing your solution.

    Best regards.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘value depending on 2 fields’ is closed to new replies.