Medium
Calculate Shopping Cart Total with Multiple Discount Types using JavaScript Reduce Method
In an e-commerce application, you need to calculate the total price of items in a shopping cart while applying various discounts. Which implementation correctly handles both percentage and fixed amount discounts?
1const cart = {2 items: [3 { id: 1, price: 100, quantity: 2 },4 { id: 2, price: 50, quantity: 1 }5 ],6 discounts: [7 { type: 'percentage', value: 10 },8 { type: 'fixed', value: 25 }9 ],10 calculateTotal() {11 const subtotal = this.items.reduce((total, item) => {12 return total + (item.price * item.quantity);13 }, 0);1415 return this.discounts.reduce((total, discount) => {16 // Implementation here17 _______________18 }, subtotal);19 }20};