Expense Summary

Calculate totals and find the largest expense from object arrays.

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

Expense Summary

Given a list of expenses, calculate useful totals and identify the largest expense. The final summary should reuse the smaller helper functions.

Write these functions:

  • calculateTotal(expenses) should return the total amount spent.

  • calculateCategoryTotal(expenses, category) should return the total for one category.

  • findLargestExpense(expenses) should return the full expense object with the largest amount.

  • createExpenseSummary(expenses) should return total, foodTotal, transportTotal, and largestExpense.

Sample checks:

js
const expenses = [  { id: 1, category: 'food', amount: 24 },  { id: 2, category: 'transport', amount: 15 },  { id: 3, category: 'food', amount: 18 },  { id: 4, category: 'books', amount: 40 },];console.log(createExpenseSummary(expenses));console.log(calculateCategoryTotal(expenses, 'food'));console.log(calculateCategoryTotal(expenses, 'health'));console.log(findLargestExpense(expenses));

Expected output:

txt
{ total: 97, foodTotal: 42, transportTotal: 15, largestExpense: { id: 4, category: "books", amount: 40 } }420{ id: 4, category: "books", amount: 40 }

findLargestExpense should return the whole expense object, not only the amount.

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.