Shopping Cart in JavaScript
Create a shopping cart
IntermediateMini ProjectsExample 11 of 20
shopping-cart.js
1class ShoppingCart {2 constructor() { this.items = []; }3 add(item) { this.items.push(item); }4 remove(id) { this.items = this.items.filter(i => i.id !== id); }5 getTotal() { return this.items.reduce((sum, item) => sum + item.price, 0); }6}
Output
// Shopping cart functionality
What's going on
Shopping Cart manages cart items and totals.