Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
1

Calculate total fee based on number of selected checkboxes & fee amount fields

Explorer ,
Jan 29, 2024 Jan 29, 2024

Screenshot 2024-01-29 004914.png

 

Hi, I have a form with fields that states the fees that are based on the mumber of runs you select. There are checkboxes for each day for you to select the runs you want to do. You can select any combination of number & type of runs each day. If you do 1 run, the amount is one amount, your second run is a lower amount, your third run is a lower amount, & so-forth.

 

I can't assign fee amounts as the values to the checkboxes, because it is dependent on how many runs you do - and you you may not always choose STD as your first run & JWW as your second run, etc - you may choose JWW as your first run & FAST as your second run, etc.

 

I also can't just put the fee amounts in my entry fee calculation, because the fee amounts may change dependending on who is hosting the event - so I need my calculation to get the value entered in those fee fields, based on the the number of checkboxes checked by day, & total the days.

 

I'm guessing the logic of my calculation needs to be something like...

 

Day 1 (ifs... checkboxes for the day total 1, then fee field 1, checkboxes for the day total 2, then fee field 1 +  field 2, checkboxes for the day total 3, then fee field 1 + field 2 + field 3, etc) + Day 2 (ifs... checkboxes for the day total 1, then fee field 1, checkboxes for the day total 2, then fee field 1 +  field 2, checkboxes for the day total 3, then fee field 1 + field 2 + field 3, etc) + Day 3 (ifs... checkboxes for the day total 1, then fee field 1, checkboxes for the day total 2, then fee field 1 +  field 2, checkboxes for the day total 3, then fee field 1 + field 2 + field 3, etc) + Day 4 (ifs... checkboxes for the day total 1, then fee field 1, checkboxes for the day total 2, then fee field 1 +  field 2, checkboxes for the day total 3, then fee field 1 + field 2 + field 3, etc)

 

... but I don't know how to do that in JS. Also, does there need to be something to prevent an error if there isn't a Day 4, or isn't a fee in the 5th/6th runs for that event? Does there need to be something in the calculation to show "$0.00" if nothing is checked?

Any help would be most appreciated - thanks!

TOPICS
JavaScript
653
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Jan 30, 2024 Jan 30, 2024

Use this:

var total = 0;
var n1 = Number(this.getField("1st").valueAsString);
var n2 = Number(this.getField("2nd").valueAsString);
var n3_4 = Number(this.getField("3rd & 4th").valueAsString);
var n5_6 = Number(this.getField("5th & 6th").valueAsString);

function incrementD(index) {
 var d = 0;
 ['STD', 'JWW', 'FAST', 'T2B', 'PremS', 'PremJ'].forEach(function(event) {
 if (this.getField(index + " " + event).valueAsString !== "Off") {
 d++;}}, this);

if (d >= 1) total += n1;
if (d >= 2) total += n2;
if (d >= 3) total += n3_4;
if (d >= 4) total += n3_4;
if (d >= 5) total += n5_6;
if (d == 6) total += n5_6;}

for (var i = 1; i <= 4; i++) {
 incrementD(i);}

event.value = total;

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 29, 2024 Jan 29, 2024

This reply was originally to correct the typos in my original post, but I now have the option to edit my posts, so I edited my OP to remove the typos & this reply is no longer needed.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 29, 2024 Jan 29, 2024

What are the field names or if you can share a file with us?

You want to show one total for all, or totals for each day separate?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 29, 2024 Jan 29, 2024

Checkbox names:

Screenshot 2024-01-29 144229.png

(+ same for Day 3 & Day 4)

 

Fee field names:

Screenshot 2024-01-29 144329.png

One total box for all days, field with text above "Entry Fee Enclosed" in original screenshot, field name "Fee".

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 29, 2024 Jan 29, 2024

If checkboxes for the day are 4 does it calculate 3rd & 4th twice or only once?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 30, 2024 Jan 30, 2024

Twice 🙂

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 30, 2024 Jan 30, 2024

Use this:

var total = 0;
var n1 = Number(this.getField("1st").valueAsString);
var n2 = Number(this.getField("2nd").valueAsString);
var n3_4 = Number(this.getField("3rd & 4th").valueAsString);
var n5_6 = Number(this.getField("5th & 6th").valueAsString);

function incrementD(index) {
 var d = 0;
 ['STD', 'JWW', 'FAST', 'T2B', 'PremS', 'PremJ'].forEach(function(event) {
 if (this.getField(index + " " + event).valueAsString !== "Off") {
 d++;}}, this);

if (d >= 1) total += n1;
if (d >= 2) total += n2;
if (d >= 3) total += n3_4;
if (d >= 4) total += n3_4;
if (d >= 5) total += n5_6;
if (d == 6) total += n5_6;}

for (var i = 1; i <= 4; i++) {
 incrementD(i);}

event.value = total;
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 30, 2024 Jan 30, 2024
LATEST

Thank you sooooo much!! That worked perfectly!! 💙

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines