Hi guys, I’m working on a coupon code function that am finding pretty damn tricky. Basically I need to write a function that takes in some variables like discount amount, percentage, minimum spend etc and spit out a discount amount. Obviously it also needs to check if the discount code entered matches the code stored in the DB.

There are two objects, one for the cart item and one for the coupon parameters. They look like this:

```
couponParams = {
id: 1,
name: '10OFF',
couponExpiry: null,
initialDiscountPercentage: 10,
initialDiscountValue: 400,
minWatermark: null,
maxWatermark: null,
maxWatermarkDiscountPercentage: null,
maxWatermarkDiscountValue: null,
renewalLength: null,
status: 'active',
}
cartItem = {
subtotal: 454.55,
tax: 45.45,
totalNoFees: 500,
total: 500
}
```

More than anything, I’m just wondering how I should set my function up to take in all the variables and make sense of them. Here is what I have so far. It doesnt really work but I’m wondering if anyone could help shine some light on the problem? Thank you

```
var calculateDiscount = function(storedCoupon, customerCoupon, options, total) {
var calculateDiscount = function(storedCoupon, customerCoupon, options, total) {
var absoluteTotal = total;
var discount;
options.initialDiscountPercentage = options.initialDiscountPercentage || 0;
options.initialDiscountValue = options.initialDiscountValue || 0;
options.minWatermark = options.minWatermark || 0;
if (customerCoupon === storedCoupon) {
getDiscountAmount();
}
options = options || 0;
if (options === 0) {
return total;
}
function getDiscountAmount() {
if (options.initialDiscountPercentage > 0 && options.initialDiscountValue > 0) {
discount = total - ((total / 100) * options.initialDiscountPercentage);
}
return discount;
}
console.log("DISCOUNT AMOUNT: ", absoluteTotal - discount);
return (absoluteTotal - discount);
}
calculateDiscount(storedCoupon, customerCoupon, couponParams, cartItem.total);
```