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/
4 changes: 4 additions & 0 deletions finance-tracker/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"semi": true,
"singleQuote": true
}
34 changes: 32 additions & 2 deletions finance-tracker/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
// This is the entrypoint for your application.
// node app.js
import {
addTransaction,
getLargestExpense,
printAllTransactions,
printSummary,
getTotalTransactions,
} from './finance.js';
Comment on lines 2 to 7

Choose a reason for hiding this comment

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

Small suggestion 🙂

Some imported functions are not used in this file.
It’s best practice to remove unused imports to keep the code clean and easier to read.


import chalk from 'chalk';

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

const transaction = {
type: 'expense',
category: 'Personal Care',
amount: 20,
description: 'baber shop',
date: '2025-01-30',
};

addTransaction(transaction);

printAllTransactions();
printSummary();

const largestExpense = getLargestExpense();
console.log(
chalk.bold(
`\nLargest Expense: ${largestExpense.category} (€${largestExpense.amount})`,
),
);

console.log(chalk.bold(`Total Transactions: ${getTotalTransactions()}`));
51 changes: 50 additions & 1 deletion finance-tracker/data.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,51 @@
// Place here the transaction data array. Use it in your application as needed.
const transactions = [];
export const transactions = [
{
id: 1,
type: 'income',
category: 'Salary',
amount: 3000,
description: 'Monthly salary',
date: '2025-01-02',
},
{
id: 2,
type: 'expense',
category: 'Rent',
amount: 1200,
description: 'Monthly rent',
date: '2025-01-05',
},
{
id: 3,
type: 'expense',
category: 'Groceries',
amount: 300,
description: 'food',
date: '2025-01-12',
},
{
id: 4,
type: 'income',
category: 'Freelance',
amount: 500,
description: 'side-income',
date: '2025-01-15',
},
{
id: 5,
type: 'expense',
category: 'Utilities',
amount: 150,
description: 'bills',
date: '2025-01-20',
},
{
id: 6,
type: 'income',
category: 'Freelance',
amount: 150,
description: 'side-income',
date: '2025-01-25',
},
];
107 changes: 92 additions & 15 deletions finance-tracker/finance.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,104 @@
function addTransaction(transaction) {
// TODO: Implement this function
import { transactions } from './data.js';
import chalk from 'chalk';

export function addTransaction(transaction) {
if (!transaction.id) {
transaction.id = transactions.length + 1;
}
transactions.push(transaction);
return transactions;
}
Comment on lines 4 to 10

Choose a reason for hiding this comment

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

I see this function expects an array of transactions, but the assignment goal is to add one transaction at a time.

What you wrote is correct, but since the function (by its name) adds a single transaction, we could simplify it to accept one transaction object instead.


export function getTotalIncome() {
let totalIncome = 0;
for (const transaction of transactions) {
if (transaction.type === 'income') {
totalIncome += transaction.amount;
}
}
return totalIncome;
}
Comment on lines +12 to 20

Choose a reason for hiding this comment

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

Nice work👍


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

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

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

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

function getLargestExpense() {
// TODO: Implement this function
export function printAllTransactions() {
console.log('All Transactions:');
let id = 1;
for (const transaction of transactions) {
const { type, category, amount, description } = transaction;

// To determine the income or expense, and to set a proper color for display
let amountColor = null;
if (transaction.type === 'income') {
amountColor = chalk.green(`€${amount}`);
} else {
amountColor = chalk.red(`€${amount}`);
}

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

console.log('\n');
}

function printAllTransactions() {
// TODO: Implement this function
}
export function printSummary() {
console.log(chalk.bold('FINANCIAL SUMMARY'));
console.log(chalk.green(`Total Income: €${getTotalIncome()}`));
console.log(chalk.red(`Total Expenses: €${getTotalExpenses()}`));
if (getBalance() >= 0) {
console.log(chalk.cyan(`Current Balance: €${getBalance()}`));
} else {
console.log(chalk.red(`Current Balance: €${getBalance()}`));
}
}

export function getTransactionsByType(type) {
let filteredTransactions = [];
for (const transaction of transactions) {
if (transaction.type === type) {
filteredTransactions.push(transaction);
}
}
return filteredTransactions;
}

export function getTotalTransactions() {
return transactions.length;
}
27 changes: 27 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.

13 changes: 13 additions & 0 deletions finance-tracker/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "finance-tracker",
"version": "1.0.0",
"description": "",
"main": "app.js",
"type": "module",
"scripts": {
"start": "node ./app.js"
},
"dependencies": {
"chalk": "^5.6.2"
}
}