Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions 01-js/easy/expenditure-analysis.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
/*
Implement a function `calculateTotalSpentByCategory` which takes a list of transactions as parameter
and return a list of objects where each object is unique category-wise and has total price spent as its value.
Transaction - an object like { itemName, category, price, timestamp }.
Output - [{ category1 - total_amount_spent_on_category1 }, { category2 - total_amount_spent_on_category2 }]

Once you've implemented the logic, test your code by running
- `npm run test-expenditure-analysis`
*/

function calculateTotalSpentByCategory(transactions) {
return [];
var spendEstimates ={};

for(var i =0; i<transactions.length;i++){
var t= transactions[i];
if(spendEstimates[t.category])
spendEstimates[t.category]+= t.price;
else
spendEstimates[t.category]=t.price;
}
var arr = Object.keys(spendEstimates);
var answer =[];
for(var j=0;j<arr.length;j++){
var category = arr[j];
var obj ={
category:category,
totalSpent:spendEstimates[arr[j]]
}
answer.push(obj);
}
return answer;
}

module.exports = calculateTotalSpentByCategory;