Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,5 @@ dist
vite.config.js.timestamp-*
vite.config.ts.timestamp-*

node_modules/

7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"semi"; true,
"singleQuote"; true,
"tabWidth"; 2,
"trailingComma"; "es5",
"printWidth"; 80
}
8 changes: 8 additions & 0 deletions finance-tracker/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
// This is the entrypoint for your application.
// node app.js

const chalk = require('chalk');
const { printAllTransactions, printSummary } = require('./finance');

console.log(chalk.bold('💰 PERSONAL FINANCE TRACKER 💰'));

printAllTransactions();
printSummary();

45 changes: 44 additions & 1 deletion finance-tracker/data.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,45 @@
// Place here the transaction data array. Use it in your application as needed.
const transactions = [];
const transactions = [
{
id: 1,
type: 'income',
category: 'salary',
amount: 3000,
description: 'Monthly salary',
date: '2025-01-15'
},
{
id: 2,
type: 'expense',
category: 'housing',
amount: 1200,
description: 'Rent',
date: '2025-01-05'
},
{
id: 3,
type: 'expense',
category: 'food',
amount: 300,
description: 'Groceries',
date: '2025-01-10'
},
{
id: 4,
type: 'income',
category: 'side-income',
amount: 500,
description: 'Freelance work',
date: '2025-01-20'
},
{
id: 5,
type: 'expense',
category: 'bills',
amount: 150,
description: 'Utilities',
date: '2025-01-18'
},
];

module.exports = transactions;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

export default transactions; is more common to use than module.exports = transactions

The way you've done it is the older CommonJS Module way. The newer way is to use export which is ES Modules way.

More info on the history: https://www.w3schools.com/nodejs/nodejs_modules_esm.asp

86 changes: 85 additions & 1 deletion finance-tracker/finance.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,111 @@
const chalk = require('chalk');
const transactions = require('./data');


function addTransaction(transaction) {
// TODO: Implement this function
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code clean up: Remove these TODO statements. Now that you have implemented the code, these are not relevant anymore.

transaction.push({...transaction}); //
}

function getTotalIncome() {
// TODO: Implement this function
let totalIncome=0;
for(const transaction of transactions){
if (transaction.type === 'income'){
totalIncome += transaction.amount;
}
}
return totalIncome;
}

function getTotalExpenses() {
// TODO: Implement this function
let totalExpenses=0;
for(const transaction of transactions){
if (transaction.type === 'expense'){
totalExpenses += transaction.amount;
}
}
return totalExpenses;
}

function getBalance() {
// TODO: Implement this function
return getTotalIncome() - getTotalExpenses();
}

function getTransactionsByCategory(category) {
// TODO: Implement this function
const result =[];
for (const transaction of transactions){
if (transaction.category === category){
result.push(transaction);
}
}
return result;
}

function getLargestExpense() {
// TODO: Implement this function
let largest = null;
for(const transaction of transactions)
if(transaction.type === 'expense'){
if(largest=== null || transaction.amount > largest.amount){
largest= transaction;
}
}
return largest;
}

function printAllTransactions() {
// TODO: Implement this function
}

console.log(chalk.bold('All transactions'));

for(const transaction of transactions){
const amountColor = transaction.type === 'income'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of the requirements is to use object destructuring. How you've implemented it works perfectly fine. This is just an opportunity to try it out and learn more about it.

More info: https://javascript.info/destructuring-assignment#object-destructuring

? chalk.green(`€${transaction.amount}`)
: chalk.red(`€${transaction.amount}`);

console.log(
`${transaction.id}. [${
transaction.type.toUpperCase()
}] ${transaction.description} - ${amountColor} (${chalk.yellow(
transaction.category
)})`
);
}

}

function printSummary(){
const totalIncome = getTotalIncome();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting: The code inside this function should be indented.

const totalExpenses = getTotalExpenses();
const balance = getBalance ();
const numberTransaction = transactions.length;
const largestExpense = getLargestExpense();

console.log(chalk.bold('\n📊 FINANCIAL SUMMARY 📊'));
console.log(chalk.bold(`Total Income: €${totalIncome}`));
console.log(chalk.bold(`Total Expenses: €${totalExpenses}`));

const balanceColor =
balance >= 0 ? chalk.cyan(`€${balance}`) : chalk.red(`€${balance}`);

console.log(chalk.bold(`Current Balance: ${balanceColor}`));
console.log(chalk.bold(`Total Transactions: ${transactions.length}`));

if (largestExpense) {
console.log(
chalk.bold(
`Largest Expense: ${largestExpense.description} (€${largestExpense.amount})`
)
);
}
}

module.exports = { printAllTransactions, printSummary };
//console.log(chalk.bold('💰 PERSONAL FINANCE TRACKER 💰'));

//printAllTransactions();
//printSummary();
86 changes: 86 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "c55-core-week-4",
"version": "1.0.0",
"description": "The week 4 assignment for the HackYourFuture Core program can be found at the following link: https://hub.hackyourfuture.nl/core-program-week-4-assignment",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/thebaraah/c55-core-week-4.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "commonjs",
"bugs": {
"url": "https://github.com/thebaraah/c55-core-week-4/issues"
},
"homepage": "https://github.com/thebaraah/c55-core-week-4#readme",
"dependencies": {
"chalk": "^4.1.2"
}
}