Trevor91
Trevor91

Reputation: 17

How to retrieve total of "else if"

How would I total these? If I add something like:

else if (cb1){ event.value[+=][1]this.getField("Totaldays").value*0;
}

It just extends the answer and doesn't give me the sum. For example:

enter image description here

else if (cb1){ event.value=this.getField("Totaldays").value*0;
}
else if (cb2){ event.value=this.getField("Totaldays").value*30; 
}
else if (cb3){ event.value=this.getField("Totaldays").value*20;
}
else if (cb4){ event.value=this.getField("Totaldays").value*0;
}
else if (cb5){ event.value=this.getField("Totaldays").value*40;
}
else if (cb6){ event.value=this.getField("Totaldays").value*0;
}
else if (c7){ event.value=this.getField("Totaldays").value*0;
}

Above is my current code which does not total the numbers, just replaces the previous number with the newly checked box number. enter image description here

Upvotes: 0

Views: 71

Answers (2)

Max Wyss
Max Wyss

Reputation: 3615

This can be simplified quite a bit. Understanding the way the return value of a checkbox works in Acrobat JavaScript is the key.

When the box is checked, it returns the specified value, when it is unchecked, it returns "Off". So, you can set the return value of the box to your rental fee. Also, assuming that the rental duration is the same for all items, the whole thing simplifies to:

var rentaldays = xx // xx is the number of days, not part of this snippet
var rsum = 0 ;
for (var i = 0 ; i < 8 ; i++) {
rsum += this.getField("cb" + i).value.toString.replace(/Off/gim , 0)*1 ;
}
var rentalfee = rentaldays * rsum ; // to be written into a field

and that's about it.

Upvotes: 0

Jonathan Wilson
Jonathan Wilson

Reputation: 4305

Use the += operator instead of + to add a number to a running total.

//...
else if (cb1){ event.value += this.getField("Totaldays").value*0; }
else if (cb2){ event.value += this.getField("Totaldays").value*30; }
else if (cb3){ event.value += this.getField("Totaldays").value*20; }
else if (cb4){ event.value += this.getField("Totaldays").value*0; }
else if (cb5){ event.value += this.getField("Totaldays").value*40; }
else if (cb6){ event.value += this.getField("Totaldays").value*0; }
else if (cb7){ event.value += this.getField("Totaldays").value*0; }

That said, there's probably a better way to do what you want to do. If you provide a little more context, I can refine my answer for you.

Consider this cleaner block of code that yields the same result as the code above but self-documents the logic and data.

let totalDays = this.getField("Totaldays").value;
[
    {checked: cb1, rentalRate:  0},
    {checked: cb2, rentalRate: 30},
    {checked: cb3, rentalRate: 20},
    {checked: cb4, rentalRate:  0},
    {checked: cb5, rentalRate: 40},
    {checked: cb6, rentalRate:  0},
    {checked: cb7, rentalRate:  0}
]
.filter(item => item.checked)
.forEach(item => {
    let rentalFee = item.rentalRate * totalDays;
    event.value += rentalFee;
});

Upvotes: 1

Related Questions