Advanced Expense Tracker with Recurring Transactions and Filters
Build a robust expense tracking function that manages one-time and recurring transactions, supports dynamic filtering by categories and date ranges, and returns detailed summaries with optimized performance.
Challenge prompt
Create a JavaScript function named `expenseTracker` that takes an array of transactions and an options object to generate a detailed expense summary. Transactions can be one-time or recurring. Each transaction object contains: `id` (string), `amount` (number), `date` (ISO string), `category` (string), and optional `recurrence` (object with `interval` in days and `endDate` as ISO string). Your function must: 1) Expand recurring transactions to all occurrences within their recurrence period. 2) Filter transactions by optional category array and date range (startDate, endDate) provided in the options. 3) Return an object with total spent, per-category spending, and filtered transaction list sorted by date ascending. 4) Handle large datasets efficiently, avoiding unnecessary computations. Ensure correct handling of edge cases such as overlapping filters and partial recurrence periods.
Guidance
- • Expand recurring transactions into individual occurrences up to the specified `endDate`, but only generate occurrences within the filter date range to optimize performance.
- • Apply filters strictly: if categories or date range filters are provided, include only matching transactions in the result and summary calculations.
- • Return results in the exact structure: { totalSpent: number, perCategory: object, transactions: array }, where `perCategory` maps category names to sums and `transactions` is sorted ascending by date.
Hints
- • Use a generator or lazy evaluation approach to handle recurring transactions efficiently, especially if the total occurrence count is large.
- • Convert date strings to Date objects once for comparisons to improve performance.
- • Be cautious with time boundaries in filtering; include transactions on the startDate and endDate dates.
Starter code
function expenseTracker(transactions, options = {}) {
// options = { categories: array of strings, startDate: ISO string, endDate: ISO string }
// Your code here
}Expected output
{ totalSpent: 350.50, perCategory: { food: 150.20, utilities: 100.00, entertainment: 100.30 }, transactions: [ /* expanded, filtered, and sorted transaction objects */ ] }
Core concepts
Challenge a Friend
Send this duel to someone else and see if they can solve it.