CodingNeed.

4 · Build useful programs · 20 MIN

Project · Expense summary

Combine loops, objects, conditions, and functions.

Build a small expense summarizer. Each expense has a category and an amount in whole currency units. Your function must sum only the requested category. First describe the input and output, then write the simplest loop, then check an empty list and a missing category.

A budget sheet totals just the rows you are interested in.

Read the example

const expenses = [{category:"food", amount:5}, {category:"travel", amount:3}];
console.log(expenses[0].amount);
Check the expected output
5

Your challenge

Input is {expenses: [{category, amount}], category}. Return the total amount for expenses whose category matches input.category. Do not mutate input.

Common trap

Adding every amount ignores the requested category.

Essential cookies keep your account signed in. Optional analytics is not configured on this site. Your choice does not affect access to lessons.

Read the Privacy Policy. You can change this choice in the footer.