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
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# .editorconfig
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,6 @@ dist
vite.config.js.timestamp-*
vite.config.ts.timestamp-*

# node modules
node_modules/

5 changes: 5 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules/
package-lock.json
dist/
build/
*.min.js
8 changes: 8 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"semi": true,
"trailingComma": "es5",
"singleQuote": true,
"printWidth": 80,
"tabWidth": 2,
"useTabs": false
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ The week 4 assignment for the HackYourFuture Core program can be found at the fo
- Implement the requirements in the `finance-tracker` folder.
- Use the existing file structure.
- You are allowed to create additional files if needed.
console.log asdsd
6 changes: 4 additions & 2 deletions finance-tracker/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// This is the entrypoint for your application.
// node app.js
const { printAllTransactions, printSummary } = require('./finance');

printAllTransactions();

printSummary();
46 changes: 44 additions & 2 deletions finance-tracker/data.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,44 @@
// 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: 'Salary',
date: '2025-01-15',
},
{
id: 2,
type: 'expense',
category: 'housing',
amount: 1200,
description: 'Rent',
date: '2025-01-20',
},
{
id: 3,
type: 'expense',
category: 'food',
amount: 300,
description: 'Groceries',
date: '2025-01-22',
},
{
id: 4,
type: 'income',
category: 'side-income',
amount: 500,
description: 'Freelance',
date: '2025-01-25',
},
{
id: 5,
type: 'expense',
category: 'bills',
amount: 150,
description: 'Utilities',
date: '2025-01-28',
},
];

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

78 changes: 69 additions & 9 deletions finance-tracker/finance.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,87 @@
function addTransaction(transaction) {
// TODO: Implement this function
const transactions = require('./data');
const chalk = require('chalk');

function addTransaction(newTransaction) {
Copy link

Choose a reason for hiding this comment

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

Good descriptive name for the parameter. This increases readability ⭐

transactions.push({ ...newTransaction });
}

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

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

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

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

function getLargestExpense() {
// TODO: Implement this function
let largest = { amount: 0, description: 'None' };
Copy link

Choose a reason for hiding this comment

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

Good way to make your function fail safe ⭐

for (const t of transactions) {
if (t.type === 'expense' && t.amount > largest.amount) {
largest = t;
}
}
return largest;
}

function printAllTransactions() {
// TODO: Implement this function
}
console.log(chalk.bold.yellow('\n💰 PERSONAL FINANCE TRACKER 💰'));
console.log('\nAll Transactions:');

transactions.forEach((t, index) => {
Copy link

Choose a reason for hiding this comment

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

Good use of foreach and accessing the index in the callback function parameter. ⭐

const { type, description, amount, category } = t;

const label =
type === 'income' ? chalk.green('[INCOME]') : chalk.red('[EXPENSE]');
const categoryStyled = chalk.yellow(category);

console.log(
`${index + 1}. ${label} ${description} - €${amount} (${categoryStyled})`
Copy link

Choose a reason for hiding this comment

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

Visually it's good to display the amount in green as well. Currently you've only displayed the type in green. Since this is a finance tracker app, it's helpful to highlight the amounts.

);
});
}

function printSummary() {
const balance = getBalance();
const balanceColor = balance >= 0 ? chalk.cyan : chalk.red;

console.log(chalk.bold('\n📊 FINANCIAL SUMMARY 📊'));
console.log(`Total Income: ${chalk.green('€' + getTotalIncome())}`);
console.log(`Total Expenses: ${chalk.red('€' + getTotalExpenses())}`);
console.log(`Current Balance: ${balanceColor('€' + balance)}`);

const largest = getLargestExpense();
console.log(`\nLargest Expense: ${largest.description} (€${largest.amount})`);
console.log(`Total Transactions: ${transactions.length}\n`);
}

module.exports = {
addTransaction,
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.

25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"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": "app.js",
"scripts": {
"format": "prettier --write .",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/noneeeed/c55-core-week-4.git"
},
"keywords": [],
"author": "Bader",
"license": "ISC",
"type": "commonjs",
"bugs": {
"url": "https://github.com/noneeeed/c55-core-week-4/issues"
},
"homepage": "https://github.com/noneeeed/c55-core-week-4#readme",
"dependencies": {
"chalk": "^4.1.2"
}
}