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
7 changes: 7 additions & 0 deletions finance-tracker/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"semi": true,
"singleQuote": true,
"trailingComma": "es5",
"tabWidth": 2,
"printWidth": 80
}
6 changes: 6 additions & 0 deletions finance-tracker/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// 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 💰\n'));
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: 'Salary',
date: '2025-01-15',
},
{
id: 2,
type: 'expense',
category: 'housing',
amount: 1200,
description: 'Rent',
date: '2025-01-01',
},
{
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',
date: '2025-01-20',
},
{
id: 5,
type: 'expense',
category: 'bills',
amount: 150,
description: 'Utilities',
date: '2025-01-25',
},
];

module.exports = transactions;
103 changes: 95 additions & 8 deletions finance-tracker/finance.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,114 @@
const chalk = require('chalk');
const transactions = require('./data');

// Add a new transaction (spread copies all fields + adds an id)
function addTransaction(transaction) {
// TODO: Implement this function
const newTransaction = { id: transactions.length + 1, ...transaction };
transactions.push(newTransaction);
return newTransaction;
}

// Add up all income (for loop)
function getTotalIncome() {
// TODO: Implement this function
let total = 0;
for (let i = 0; i < transactions.length; i++) {
if (transactions[i].type === 'income') {
total += transactions[i].amount;
}
}
return total;
}

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

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

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

// Find the expense with the highest amount (while loop)
function getLargestExpense() {
// TODO: Implement this function
let largest = null;
let i = 0;
while (i < transactions.length) {
const transaction = transactions[i];
if (
transaction.type === 'expense' &&
(largest === null || transaction.amount > largest.amount)
) {
largest = transaction;
}
i++;
}
return largest;
}

// Print every transaction with colors (uses object destructuring)
function printAllTransactions() {
// TODO: Implement this function
}
console.log(chalk.bold('All Transactions:'));
transactions.forEach((transaction, index) => {
const { type, category, amount, description } = transaction;
const color = type === 'income' ? chalk.green : chalk.red;
console.log(
`${index + 1}. [${type.toUpperCase()}] ${description} - ${color(
'€' + amount
)} (${chalk.yellow(category)})`
);
});
}

// Print the financial summary report
function printSummary() {
const balance = getBalance();
const balanceColor = balance < 0 ? chalk.red : chalk.cyan;
const largest = getLargestExpense();

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

if (largest) {
console.log(
`\n${chalk.bold('Largest Expense:')} ${largest.description} (${chalk.red(
'€' + largest.amount
)})`
);
}
console.log(chalk.bold('Total Transactions: ') + transactions.length);
}

module.exports = {
addTransaction,
getTotalIncome,
getTotalExpenses,
getBalance,
getTransactionsByCategory,
getLargestExpense,
printAllTransactions,
printSummary,
};
105 changes: 105 additions & 0 deletions finance-tracker/package-lock.json

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

19 changes: 19 additions & 0 deletions finance-tracker/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "finance-tracker",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "commonjs",
"dependencies": {
"chalk": "^4.1.2"
},
"devDependencies": {
"prettier": "^3.10.1"
}
}