### Post

3 followers Follow

# How to calculate final price using different and multiple discount categories

I am very new to JavaScript and am having difficulty setting up the follow calculation. I am tracking expenses of a renovation. When I purchase supplies, the discounts I get vary, and sometimes get more than one at a time. I am trying to set up a category function where I can select which discounts applied to my purchase.

Category 1 = 20 percent discount
Category 2 = 15 percent discount
Category 3 = 10 percent discount
Category 4 = 2 percent discount

I start by enter a 'money' field entitled "Price before discount." Then I would like to select the applicable discounts list above from the field "Discount Applied," and end up with a "Final Price."

So, If I had a \$100 purchase price before discounts, and then selected "20 percent discount," I would like the calculation to multiply @Price Before Discount by 0.80 to reflect the "Final Price."

In addition, I will sometimes get multiple discounts. One from the supplier (e.g. 20%) and one from a credit card (e.g. 2%). They are not applied at the same time, so I can not simply use a 12 percent discount button. Using this scenario, I need to apply the 20% discount first, and then a 2% discount on the remaining amount. (e.g. \$100*0.80 = \$80; then \$80*0.98 = \$78.40).

I have tried using several different examples from Podio's tips and from the forum, but I can't seem to make them work for what I have described above. I keep getting a syntax error, unexpected token ILLEGAL. Any help or advice would be greatly appreciated! Thanks!

That may not be the slickest way, but it seems to work:

``````var initialPrice = @Price before discount;

var strDiscount = @Discount.join(",").replace("Category 1", 0.8).replace("Category 2", 0.85).replace("Category 3", 0.9).replace("Category 4", 0.98)

var arrDiscount = strDiscount.split(",")

var discount = 1;

for (var i=0; i<arrDiscount.length; i++) {
discount = discount * arrDiscount[i];
}

discount * initialPrice
``````