Skip to main content
Participant
October 13, 2016
Answered

Custom Javascript

  • October 13, 2016
  • 1 reply
  • 505 views

I have a column labeled "Number of Round Trips" for a monthly mileage report.  Each field is labeled Trips1 through Trips31.  I need to be able to calculate the number of days the vehicle is used.  It would be an easy calculation if there were 30 - round trips and only one - 1/2 trip.  How would one configure the JavaScript so that each 1/2 trip calculates as 1 day?   For example, how can I calculate 27- round trips and 4 - 1/2 trips to be 31 days and not 29 days?

This topic has been closed for replies.
Correct answer Karl Heinz Kremer

There are different ways you can do that. Can I assume that there is a maximum of one round trip per day?

If so, you could use the following script as your calculation script in your "totals" field:

var cnt = 0;

for (var i=0; i<=31; i++) {

    var f = this.getField("Trips" + i);

    if (f != null) {

        if (Number(f.value) > 0)

            cnt++;

    }

}

event.value = cnt;

This will count anything that is larger than 0 as a day the vehicle was used.

1 reply

Karl Heinz  Kremer
Community Expert
Karl Heinz KremerCommunity ExpertCorrect answer
Community Expert
October 13, 2016

There are different ways you can do that. Can I assume that there is a maximum of one round trip per day?

If so, you could use the following script as your calculation script in your "totals" field:

var cnt = 0;

for (var i=0; i<=31; i++) {

    var f = this.getField("Trips" + i);

    if (f != null) {

        if (Number(f.value) > 0)

            cnt++;

    }

}

event.value = cnt;

This will count anything that is larger than 0 as a day the vehicle was used.

Participant
October 13, 2016

Thank you so very much for taking the time to help me!

It works perfectly☺

Michelle Sylvestre, (Personal/Business information removed. Email signatures and personal information )