How do you calculate the cart total in a food ordering app?
Sum price * quantity for each item in the cart: Object.entries(cart).reduce((sum, [id, qty]) => { const item = menu.find(m => m.id === parseInt(id)); return sum + item.price * qty; }, 0).
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in How to Build a Food Ordering App in a Machine Coding Interview
Create a menu array, a cart object (item ID to quantity), search and filters. Render the filtered menu and cart sidebar. Handle add/remove from cart, quantity changes, and total calculation. Handle empty cart and no results. This is a real Swiggy interview question.
Use an object mapping item ID to quantity: cart = { 1: 2, 3: 1 }. addToCart increments the quantity. removeFromCart deletes the key. Calculate total by summing price * qty for each item in the cart.
Filter the menu array based on search query (name includes query), veg/non-veg flag, category, and price range. Re-render the menu on every filter change. Show 'No items found' if the filtered list is empty.
Still have questions?
Browse all our FAQs or reach out to our support team
