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/
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,
"tabWidth": 2,
"trailingComma": "es5",
"printWidth": 80
}
15 changes: 13 additions & 2 deletions finance-tracker/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
// This is the entrypoint for your application.
// node app.js
import { transactions } from './data.js';

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This import here will not work because of the module styles: CommonJS. Check my comment on the package.json file.

import {
addTransaction,
getTotalIncome,
getTotalExpenses,
getBalance,
getTransactionsByCategory,
getLargestExpense,
printAllTransactions,
printSummary,
} from './finance.js';
Comment on lines +2 to +11

Copy link
Copy Markdown

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.


printAllTransactions(transactions);
printSummary(transactions);
86 changes: 84 additions & 2 deletions finance-tracker/data.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,84 @@
// 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-12-28',
},
{
id: 2,
type: 'expense',
category: 'rent',
amount: 800,
description: 'housing',
date: '2026-01-01',
},
{
id: 3,
type: 'expense',
category: 'utility',
amount: 250,
description: 'bills',
date: '2026-01-16',
},
{
id: 4,
type: 'expense',
category: 'groceries',
amount: 180,
description: 'food',
date: '2026-01-26',
},
{
id: 5,
type: 'income',
category: 'benefits',
amount: 200,
description: 'huurtoeslag',
date: '2026-01-20',
},
{
id: 6,
type: 'expense',
category: 'insurance',
amount: 210,
description: 'bills',
date: '2026-01-24',
},
{
id: 7,
type: 'income',
category: 'salary',
amount: 350,
description: 'freelance',
date: '2026-01-19',
},
{
id: 8,
type: 'expense',
category: 'transport',
amount: 80,
description: 'bills',
date: '2026-01-30',
},
{
id: 9,
type: 'expense',
category: 'entertainment',
amount: 60,
description: 'pc-games',
date: '2026-01-01',
},
{
id: 10,
type: 'expense',
category: '',
amount: 200,
description: '',
date: '2026-01-20',
},
];

export default transactions;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

data.js uses a default export, so it should be imported without {}.

86 changes: 72 additions & 14 deletions finance-tracker/finance.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,85 @@
function addTransaction(transaction) {
// TODO: Implement this function
import transactions from `./data.js`;
import chalk from 'chalk';

export function addTransaction(transaction) {
transactions.push({ ...transaction });
}

function getTotalIncome() {
// TODO: Implement this function
export function getTotalIncome(transactions) {
let totalIncome = 0;
for (const transaction of transactions) {
if (transaction.type === `income`) {
totalIncome += transaction.amount;
}
}
return totalIncome;
}

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

function getBalance() {
// TODO: Implement this function
export function getBalance() {
return getTotalIncome() - getTotalExpenses();
}
Comment on lines +28 to 30

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There is a logic issue in getBalance

getTotalIncome and getTotalExpenses require transactions,
but they are called here without passing any arguments.


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

function getLargestExpense() {
// TODO: Implement this function

export function getLargestExpense() {
let largest = null;
for (const transaction of transactions) {
if (transaction.type === 'expense' && (!largest || transaction.amount > largest.amount)) {
largest = transaction;
}
}
return largest;
}

function printAllTransactions() {
// TODO: Implement this function
export function printAllTransactions(transactions) {
console.log(chalk.bold.cyan('PERSONAL FINANCE TRACKER\n'));
console.log(chalk.bold('All Transactions:\n'));

transactions.forEach((transaction, index) => {
const { id, type, category, amount, description } = transaction;
const typeLabel = type === 'income' ? '[INCOME]' : '[EXPENSE]';
const coloredAmount =
type === 'income' ? chalk.green(`€${amount}`) : chalk.red(`€${amount}`);
const coloredCategory = chalk.yellow(`(${category})`);

console.log(`${index + 1}. ${typeLabel} ${description} - ${coloredAmount} ${coloredCategory}`);
});

export function printSummary(transactions) {
const totalIncome = getTotalIncome(transactions);
const totalExpenses = getTotalExpenses(transactions);
const balance = getBalance(transactions);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Because getBalance does not accept parameters, the balance calculation here will not be correct.

const largestExpense = getLargestExpense(transactions);
const transactionCount = transactions.length;

console.log(chalk.bold.cyan('FINANCIAL SUMMARY\n'));
console.log(`Total Income: ${chalk.green(`€${totalIncome}`)}`);
console.log(`Total Expenses: ${chalk.red(`€${totalExpenses}`)}`);
const balanceColor = balance >= 0 ? chalk.cyan : chalk.red;
console.log(`Current Balance: ${balanceColor(`€${balance}`)}`);

if (largestExpense) {
console.log(
`\nLargest Expense: ${largestExpense.description} (${chalk.red(`€${largestExpense.amount}`)})`);
}
console.log(`Total Transactions: ${chalk.bold(transactionCount)}\n`);
}
28 changes: 28 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": "hyf-assignment",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "commonjs",

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You mix two module styles: CommonJS and ES modules. If you use commonjs you need to use require.

Or you can enable ES modules ("type": "module"), and then also imports must match how values are exported.

For example, in data.js, which uses a default export, it should be imported without curly braces:
import transactions from './data.js';

"dependencies": {
"chalk": "^5.6.2"
}
}