Cart Total Calculator

Summarize cart totals from arrays of item objects.

Start building, submit solution and get feedback from the community.
2Submit Solution
5 upvotes10 upvotes

Cart Total Calculator

A cart contains items with a price and quantity. Build a summary that calculates subtotal, discount, tax, and final total.

Write these functions:

  • calculateSubtotal(items) should add price * quantity for every item.

  • calculateDiscount(subtotal, discountPercent) should return the discount amount.

  • calculateTax(amountAfterDiscount, taxPercent) should return the tax amount after the discount.

  • createCartSummary(items, discountPercent, taxPercent) should return an object with subtotal, discount, tax, and total.

Sample checks:

js
const cartItems = [  { name: 'Notebook', price: 10, quantity: 2 },  { name: 'Pen', price: 2, quantity: 5 },  { name: 'Bag', price: 30, quantity: 1 },];console.log(createCartSummary(cartItems, 10, 5));console.log(calculateSubtotal(cartItems));const singleItemCart = [{ name: 'Mouse', price: 25, quantity: 2 }];console.log(createCartSummary(singleItemCart, 0, 10));

Expected output:

txt
{ subtotal: 60, discount: 6, tax: 2.7, total: 56.7 }60{ subtotal: 50, discount: 0, tax: 5, total: 55 }

The subtotal must include every item and multiply each item's price by its quantity.

Join the Community

roadmap.sh is the most starred project on GitHub and is visited by hundreds of thousands of developers every month.

Rank  out of 28M!

361K

GitHub Stars

Star us on GitHub
Help us reach #1

+90k every month

+2.1M

Registered Users

Register yourself
Commit to your growth

+2k every month

49K

Discord Members

Join on Discord
Join the community

Roadmaps Best Practices Guides Videos FAQs YouTube

roadmap.sh by @kamrify

Community created roadmaps, best practices, projects, articles, resources and journeys to help you choose your path and grow in your career.

ThewNewStack

The top DevOps resource for Kubernetes, cloud-native computing, and large-scale development and deployment.