Medium
Calculate Shopping Cart Total with Category-Based Discounts using JavaScript Reduce
You’re implementing a shopping cart for an e-commerce platform. You need to calculate the total cost of items while applying a discount to certain categories. Complete the function to correctly calculate the cart total.
1const cart = [2 { item: 'Laptop', price: 1200, category: 'electronics' },3 { item: 'Shoes', price: 100, category: 'apparel' },4 { item: 'Headphones', price: 300, category: 'electronics' },5];67function calculateTotal(cart) {8 return cart.reduce((total, { price, category }) => {9 const discount = category === 'electronics' ? 0.9 : 1;10 // Fill in the missing code11 return total + ____________________;12 }, 0);13}1415console.log(calculateTotal(cart)); // Expected Output: ?