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
File renamed without changes.
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,
"tabWidth": 2,
"trailingComma": "none"
}
1 change: 0 additions & 1 deletion finance-tracker/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
// This is the entrypoint for your application.
// node app.js

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: 'Monthly salary',
date: '2025-01-15'
},
{
id: 2,
type: 'expense',
category: 'groceries',
amount: 1000,
description: 'Weekly groceries shopping',
date: '2025-01-16'
},
{
id: 3,
type: 'expense',
category: 'rent',
amount: 1500,
description: 'Monthly rent payment',
date: '2025-01-30'
},
{
id: 4,
type: 'income',
category: 'freelance',
amount: 500,
description: 'Freelance project payment',
date: '2025-01-20'
},
{
id: 5,
type: 'expense',
category: 'entertainment',
amount: 75,
description: 'Movie tickets',
date: '2025-01-18'
}
];

module.exports = transactions;
Copy link

Choose a reason for hiding this comment

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

export default transactions; is more common to use than module.exports = transactions

The way you've done it is the older CommonJS Module way. The newer way is to use export which is ES Modules way.

More info on the history: https://www.w3schools.com/nodejs/nodejs_modules_esm.asp

134 changes: 131 additions & 3 deletions finance-tracker/finance.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,155 @@
/***2. Required Functions**

Implement these functions using appropriate loops and array methods:

- `addTransaction(transaction)` - Add new transaction to array
- `getTotalIncome()` - Sum all income using a loop
- `getTotalExpenses()` - Sum all expenses using a loop
- `getBalance()` - Calculate total income minus expenses
- `getTransactionsByCategory(category)` - Filter transactions (use loop + push or indexOf/includes)
- `getLargestExpense()` - Find highest expense amount
- `printAllTransactions()` - Display all transactions with formatting*/

const transactions = require('./data.js');
const chalk = require('chalk');
function addTransaction(transaction) {
// TODO: Implement this function
Copy link

Choose a reason for hiding this comment

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

Code clean up: Remove these TODO statements. Now that you have implemented the code, these are not relevant anymore.

const newTransaction = {
...transaction,
id: transactions.length + 1
};
transactions.push(newTransaction);
}

function getTotalIncome() {
// TODO: Implement this function
let total = 0;
for (let i = 0; i < transactions.length; i++) {
Copy link

Choose a reason for hiding this comment

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

This for loop is perfectly fine and works. But you can also use the for...of loop as well. This makes the code more readable and removes the need to use a variable i to keep track of the index.

Learn more: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of

if (transactions[i].type === 'income') {
//total = transactions[i].amount + total;
total += transactions[i].amount;
}
}
return total;
}

function getTotalExpenses() {
// TODO: Implement this function
let total = 0;
//for(const transaction of transactions)
for (let i = 0; i < transactions.length; i++) {
Copy link

Choose a reason for hiding this comment

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

for...of loop can also be used here.

if (transactions[i].type === 'expense') {
total = transactions[i].amount + total;
}
}
return total;
}

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

function getTransactionsByCategory(category) {
// TODO: Implement this function
const result = [];
for (let i = 0; i < transactions.length; i++) {
Copy link

Choose a reason for hiding this comment

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

for...of loop can also be used here.

if (transactions[i].category === category) {
result.push(transactions[i]);
}
}
return result;
}

function getLargestExpense() {
// TODO: Implement this function
let largest = null;

for (let i = 0; i < transactions.length; i++) {
if (transactions[i].type === 'expense') {
if (largest === null || transactions[i].amount > largest.amount) {
largest = transactions[i];
}
}
}

return largest;
}

function printAllTransactions() {
// TODO: Implement this function
}
console.log(chalk.cyan.bold('\n 💰 PERSONAL FINANCE TRACKER 💰\n'));

for (let i = 0; i < transactions.length; i++) {
const transaction = transactions[i];
const { id, type, description, amount, category } = transaction;
Copy link

Choose a reason for hiding this comment

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

Good use of object destructuring ⭐

const typeDisplay =
type === 'income'
? chalk.green.bold('[income]')
: chalk.red.bold('[expense]');

const amountDisplay =
type === 'income' ? chalk.green(`€${amount}`) : chalk.red(`€${amount}`);

console.log(
`${chalk.yellow(id)}. ${typeDisplay} ${description} - ${amountDisplay} ` +
`(${chalk.yellow(category)})`
);
}
}

function printSummary() {
const totalIncome = getTotalIncome();
const totalExpenses = getTotalExpenses();
const balance = getBalance();
const largestExpense = getLargestExpense();
const transactionCount = transactions.length;

console.log(chalk.cyan.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 : chalk.red;
console.log(chalk.bold(`Current Balance: ${balanceColor(`€${balance}`)}`));

if (largestExpense) {
console.log(
chalk.bold(
`Largest Expense: ${largestExpense.description} (` +
`${chalk.red(`€${largestExpense.amount}`)})`
)
);
}

console.log(chalk.bold(`Total Transactions : ${transactionCount}`));
}

function removeTransaction(id) {
Copy link

Choose a reason for hiding this comment

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

Well done on implementing a bonus challenge ⭐

let indexToRemove = -1;
let i = 0;

while (i < transactions.length) {
if (transactions[i].id === id) {
indexToRemove = i;
break;
}
i++;
}

if (indexToRemove !== -1) {
return transactions.splice(indexToRemove, 1)[0];
}
return null;
}
module.exports = {
addTransaction,
getTotalIncome,
getTotalExpenses,
getBalance,
getTransactionsByCategory,
getLargestExpense,
printAllTransactions,
printSummary,
removeTransaction
};
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.

27 changes: 27 additions & 0 deletions finance-tracker/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/mareh-aboghanem/c55-core-week-4.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "commonjs",
"bugs": {
"url": "https://github.com/mareh-aboghanem/c55-core-week-4/issues"
},
"homepage": "https://github.com/mareh-aboghanem/c55-core-week-4#readme",
"dependencies": {
"chalk": "^4.1.2"
},
"devDependencies": {
"prettier": "^3.8.1"
}
}
4 changes: 4 additions & 0 deletions finance-tracker/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const { printAllTransactions, printSummary } = require('./finance.js');

printAllTransactions();
Copy link

Choose a reason for hiding this comment

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

Although this is fine to use run.js as the entry point of the app, usually app.js is used as the entry point.

printSummary();