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: 27 additions & 2 deletions 01-js/easy/expenditure-analysis.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,34 @@
Once you've implemented the logic, test your code by running
- `npm run test-expenditure-analysis`
*/
function implement(arr, category, price){
let flag = false;
for(let item of arr){
for(let key in item){
if(key === category){
item[category]+=price;
flag = true;
break;
}
}
}
if(!flag){
let obj = new Object();
obj[category] = price;
arr.push(obj);
}
}

function calculateTotalSpentByCategory(transactions) {
return [];
let arr = new Array();
for(let key of transactions){
implement(arr, key.category, key.price)
}
return arr;
}

module.exports = calculateTotalSpentByCategory;
module.exports = {
calculateTotalSpentByCategory,
implement
};