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
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"semi": true,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "es5"
}
142 changes: 134 additions & 8 deletions finance-tracker/finance.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,153 @@


const chalk = require('chalk');


let 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-16',
},
{
id: 3,
type: 'expense',
category: 'food',
amount: 300,
description: 'Groceries',
date: '2025-01-17',
},
{
id: 4,
type: 'income',
category: 'side-income',
amount: 500,
description: 'Freelance project',
date: '2025-01-20',
},
{
id: 5,
type: 'expense',
category: 'bills',
amount: 150,
description: 'Utilities',
date: '2025-01-22',
},
];


// Add a new transaction
function addTransaction(transaction) {
// TODO: Implement this function
transactions = [...transactions, transaction];
console.log(chalk.green(`Transaction added: ${transaction.description} (€${transaction.amount})`));
}

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

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

// Get current balance
function getBalance() {
// TODO: Implement this function
const balance = getTotalIncome() - getTotalExpenses();
return balance;
}

// Get transactions by category (demonstrates loop + push + includes)
function getTransactionsByCategory(category) {
// TODO: Implement this function
const filtered = [];
for (const tx of transactions) {
if (tx.category.toLowerCase().includes(category.toLowerCase())) {
filtered.push(tx);
}
}
return filtered;
}

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

// Print all transactions
function printAllTransactions() {
// TODO: Implement this function
}
console.log(chalk.bold('\nAll Transactions:'));
for (const tx of transactions) {
const { id, type, category, amount, description } = tx;
const typeLabel = type === 'income' ? chalk.green('[INCOME]') : chalk.red('[EXPENSE]');
console.log(`${id}. ${typeLabel} ${description} - €${amount} (${chalk.yellow(category)})`);
Comment on lines +109 to +110
Copy link

Choose a reason for hiding this comment

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

I think the amount should've been red/green and not the text "income"/"expense".

}
}

// Print summary report
function printSummary() {
const totalIncome = getTotalIncome();
const totalExpenses = getTotalExpenses();
const balance = getBalance();
const largestExpense = getLargestExpense();
console.log(chalk.bold('\n📊 FINANCIAL SUMMARY 📊'));
console.log(`Total Income: ${chalk.green.bold('€' + totalIncome)}`);
console.log(`Total Expenses: ${chalk.red.bold('€' + totalExpenses)}`);
console.log(
`Current Balance: ${
balance >= 0 ? chalk.cyan.bold('€' + balance) : chalk.red.bold('€' + balance)
}`
);
if (largestExpense) {
console.log(
`Largest Expense: ${largestExpense.description} (${chalk.red.bold('€' + largestExpense.amount)})`
);
}
console.log(`Total Transactions: ${transactions.length}`);
}



printAllTransactions();

printSummary();

addTransaction({
id: 6,
type: 'expense',
category: 'entertainment',
amount: 200,
description: 'Movie and snacks',
date: '2025-01-25',
});


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

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

27 changes: 27 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"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/MunaNasher/c55-core-week-4.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "commonjs",
"bugs": {
"url": "https://github.com/MunaNasher/c55-core-week-4/issues"
},
"homepage": "https://github.com/MunaNasher/c55-core-week-4#readme",
"dependencies": {
"chalk": "^4.1.2"
},
"devDependencies": {
"prettier": "^3.8.1"
}
}