Skip to content

Commit

Permalink
still need to update calculateTransactions but got a plan!
Browse files Browse the repository at this point in the history
  • Loading branch information
rnaksdl committed Apr 13, 2024
1 parent 053f026 commit 498239c
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 13 deletions.
Binary file modified .DS_Store
Binary file not shown.
Binary file added JS_Playground/.DS_Store
Binary file not shown.
17 changes: 17 additions & 0 deletions JS_Playground/budget_helper/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="script.js" defer></script>
</head>
<body>
<h1>Copy and paste here!</h1>
<input type="text" class="transactions">
<button onclick="display()">Format</button>
<p class="output">

</p>
</body>
</html>
85 changes: 74 additions & 11 deletions JS_Playground/budget_helper/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,92 @@ then parse the string
NOT making objects
BUT just calculating info on the spot as we are parsing through the data.
1. we need to divide input up monthly
2. for each month we need to divide it up by source
3. for each source we need to divide it up by category
4. then calculate sum of each category, sum of each source, sum of each month,
0. we need to divide input up yearly
1. for each year, divide it up monthly
2. for each month, divide it up by source
3. for each source, divide it up by category
4. then calculate these
total income of each sources monthy
total combined income monthy
total expenditure of Needs monthy
total expenditure of Wants monthy
total expenditure of each sources monthy
total combined expenditure
total balance left on each source (income - expenditure)
total combined balanced
*/





function calculateTransactions(input) {
const transactions = input.split('\n');

const result = transactions.reduce((acc, transaction) => {
const [date, source, category, amountStr] = transaction.split(',');
const amount = parseInt(amountStr);
const [month, day, year] = date.split('/');

acc[year] = acc[year] || {};
acc[year][month] = acc[year][month] || {};
acc[year][month][source] = acc[year][month][source] || {};
acc[year][month][source][category] = (acc[year][month][source][category] || 0) + amount;

return acc;
}, {});

const calculatedResult = {};

for (const year in result) {
calculatedResult[year] = {};
let yearlySum = 0;

for (const month in result[year]) {
calculatedResult[year][month] = {};
let monthlySum = 0;

for (const source in result[year][month]) {
calculatedResult[year][month][source] = {};
let sourceSum = 0;

for (const category in result[year][month][source]) {
const categorySum = result[year][month][source][category];
calculatedResult[year][month][source][category] = categorySum;
sourceSum += categorySum;
}

calculatedResult[year][month][source].sum = sourceSum;
monthlySum += sourceSum;
}

calculatedResult[year][month].sum = monthlySum;
yearlySum += monthlySum;
}

calculatedResult[year].sum = yearlySum;
}

return calculatedResult;
}


function display() {
const result = calculateTransactions(document.querySelector(".transactions").value);
document.querySelector(".output").innerHTML = result;
}



// get user input
const input = "04/12/2024,Dad,Wants,-25\n04/12/2024,Claire,Wants,-22";

// split input into array of lines separated by new line
const lines = input.split('\n');



for (const line of lines) {
const [date, source, amount, category] = line.split(',');


}
/*
calculateTransactions("04/12/2024,Dad,Wants,-25\n04/12/2024,Claire,Wants,-22\n04/12/2024,Dad,Income,1000\n04/12/2024,Claire,Needs,-50\n05/01/2024,Dad,Wants,-30\n05/01/2024,Claire,Wants,-40")
'04/12/2024,Dad,Wants,-25 04/12/2024,Claire,Wants,-22 04/12/2024,Dad,Income,1000 04/12/2024,Claire,Needs,-50 05/01/2024,Dad,Wants,-30 05/01/2024,Claire,Wants,-40'
*/
8 changes: 6 additions & 2 deletions JS_Playground/budget_helper/transaction.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
04/12/2024,Dad,-25,Wants
04/12/2024,Claire,-22,Wants
04/12/2024,Dad,Wants,-25
04/12/2024,Claire,Wants,-22
04/12/2024,Dad,Income,1000
04/12/2024,Claire,Needs,-50
05/01/2024,Dad,Wants,-30
05/01/2024,Claire,Wants,-40

0 comments on commit 498239c

Please sign in to comment.