Medium
This question is a daily question and will count towards your daily streak.
You're debugging a shopping cart implementation where prices are acting strangely. What surprising behavior might occur with this code?
1class ShoppingCart {2 items = new Map();34 addItem(product, quantity = 1) {5 const currentQty = this.items.get(product) || 0;6 this.items.set(product, currentQty + quantity);7 }89 getTotalPrice() {10 let total = 0;11 for (let [product, quantity] of this.items) {12 total += product.price * quantity;13 }14 return total.toFixed(2);15 }16}1718const product1 = { name: 'Widget', price: 9.99 };19const product2 = { name: 'Gadget', price: 19.99 };2021const cart = new ShoppingCart();22cart.addItem(product1, 2);23cart.addItem(product2, 1);2425product1.price = 14.99;26console.log(cart.getTotalPrice());