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
1 change: 1 addition & 0 deletions finance-tracker/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
6 changes: 6 additions & 0 deletions finance-tracker/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"semi": true,
"singleQuote": true,
"trailingComma": "es5",
"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 finance = require('./finance');

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

finance.printAllTransactions();
finance.printSummary();
46 changes: 45 additions & 1 deletion finance-tracker/data.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,46 @@
// Place here the transaction data array. Use it in your application as needed.
const transactions = [];
const transactions = [
// Array of transaction objects
{
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-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 work',
date: '2025-01-20',
},
{
id: 5,
type: 'expense',
category: 'bills',
amount: 150,
description: 'Utilities',
date: '2025-01-18',
},
];

module.exports = transactions;
88 changes: 87 additions & 1 deletion finance-tracker/finance.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,113 @@
const chalk = require("chalk");
const transactions = require("./data.js");

function addTransaction(transaction) {
// TODO: Implement this function
transactions.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 (let i=0; i<transactions.length; i++) {
if (transactions[i].type === 'expense') {
totalExpenses += transactions[i].amount;
}
}
return totalExpenses;
}

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

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

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

function printAllTransactions() {
// TODO: Implement this function
}
console.log(chalk.bold('\nAll Transactions:\n'));
for (let i=0; i<transactions.length; i++) {
const transaction = transactions[i];
let amount;
if (transaction.type === 'income') {
amount = chalk.green(`+ $${transaction.amount}`);
}
else {
amount = chalk.red(`- $${transaction.amount}`);
}
console.log(
`${i + 1}.[${transaction.type.toUpperCase()}]
${transaction.description
}- ${amount}(${chalk.yellow(transaction.category)})`
Comment on lines +72 to +75
Copy link

Choose a reason for hiding this comment

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

This should fix the formatting issues:

Suggested change
console.log(
`${i + 1}.[${transaction.type.toUpperCase()}]
${transaction.description
}- ${amount}(${chalk.yellow(transaction.category)})`
console.log(
`${i + 1}.[${transaction.type.toUpperCase()}] ${transaction.description}- ${amount} (${chalk.yellow(transaction.category)})`

);
}

}
function printSummary() {
const totalIncome = getTotalIncome();
const totalExpenses = getTotalExpenses();
const balance = getBalance();
const largestExpense = getLargestExpense();

console.log(chalk.bold('\n📊 FINANCIAL SUMMARY 📊\n'));

console.log(chalk.bold('Total Income:'), chalk.green(`€${totalIncome}`));
console.log(chalk.bold('Total Expenses:'), chalk.red(`€${totalExpenses}`));

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

console.log(chalk.bold('Current Balance:'), balanceColor);

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

console.log(chalk.bold('Total Transactions:'), transactions.length);
}

module.exports = {
addTransaction,
getTotalIncome,
getTotalExpenses,
getBalance,
getTransactionsByCategory,
getLargestExpense,
printAllTransactions,
printSummary,
};
86 changes: 86 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.

16 changes: 16 additions & 0 deletions finance-tracker/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"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"
}
}