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
6 changes: 6 additions & 0 deletions finance-tracker/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"trailingComma": "es5",
"tabWidth": 4,
"semi": false,
"singleQuote": true
}
22 changes: 22 additions & 0 deletions finance-tracker/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
// This is the entrypoint for your application.
// node app.js

import {
addTransaction,
printAllTransactions,
printSummary,
} from './finance.js'

const newTransaction = {
id: 6,
type: 'income',
category: 'freelance',
amount: 500,
description: 'Freelance project',
date: '2025-01-20',
}

addTransaction(newTransaction)
Copy link

Choose a reason for hiding this comment

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

Nice job using this function ⭐


// Display all transactions
printAllTransactions()

// Display financial summary
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: 'Monthly salary',
date: '2025-01-15',
},
{
id: 2,
type: 'expense',
category: 'groceries',
amount: 150,
description: 'Weekly groceries',
date: '2025-01-16',
},
{
id: 3,
type: 'expense',
category: 'utilities',
amount: 120,
description: 'Electricity bill',
date: '2025-01-17',
},
{
id: 4,
type: 'expense',
category: 'entertainment',
amount: 50,
description: 'Movie tickets',
date: '2025-01-18',
},
{
id: 5,
type: 'income',
category: 'freelance',
amount: 500,
description: 'Freelance project',
date: '2025-01-19',
},
]

export default transactions
106 changes: 98 additions & 8 deletions finance-tracker/finance.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,117 @@
import chalk from 'chalk'
const { bold, green, red, yellow, cyan } = chalk
import transactions from './data.js'

// Add a new transaction. Create a shallow copy using the spread
// operator so the original object passed in isn't mutated by reference.
function addTransaction(transaction) {
// TODO: Implement this function
transactions.push({ ...transaction })
}

function getTotalIncome() {
// TODO: Implement this function
let totalIncome = 0
// Use for...of and object destructuring for clarity
for (const transactionItem of transactions) {
const { type, amount } = transactionItem
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 ⭐

if (type === 'income') {
totalIncome += amount
}
}
return totalIncome
}

function getTotalExpenses() {
// TODO: Implement this function
let totalExpenses = 0
// for...of loop with destructuring improves readability
for (const transactionItem of transactions) {
const { type, amount } = transactionItem
if (type === 'expense') {
totalExpenses += amount
}
}
return totalExpenses
}

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

function getTransactionsByCategory(category) {
// TODO: Implement this function
const filteredTransactions = []
// Use for...of and destructuring to collect matching entries
for (const transactionItem of transactions) {
const { category: transactionCategory } = transactionItem
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 assign a property to a variable with another name, so that it does not conflict with the parameter name ⭐

if (transactionCategory === category) {
filteredTransactions.push(transactionItem)
}
}
return filteredTransactions
}

function getLargestExpense() {
// TODO: Implement this function
let largestAmount = 0
// Iterate with destructuring to find the maximum expense amount
for (const transactionItem of transactions) {
const { type, amount } = transactionItem
if (type === 'expense' && amount > largestAmount) {
largestAmount = amount
}
}
return largestAmount
}

function printAllTransactions() {
// TODO: Implement this function
}
console.log(bold('💰 PERSONAL FINANCE TRACKER 💰\n'))
console.log(bold('All Transactions:'))
// Use for...of for clearer iteration
for (const transactionItem of transactions) {
const { id, type, description, amount, category } = transactionItem
const typeDisplay = type.toUpperCase()
const amountColor = type === 'income' ? green : red
const categoryColor = yellow
console.log(
`${id}. [${typeDisplay}] ${description} - ${amountColor(`€${amount}`)} (${categoryColor(category)})`
)
}
console.log()
}

function printSummary() {
const totalIncome = getTotalIncome()
const totalExpenses = getTotalExpenses()
const balance = getBalance()
const largestExpenseAmount = getLargestExpense()

// Find the largest expense transaction's description. We iterate
// and compare amounts; stop early when we find the matching entry.
let largestExpenseDescription = 'N/A'
for (const transactionItem of transactions) {
const { type, amount, description } = transactionItem
if (type === 'expense' && amount === largestExpenseAmount) {
largestExpenseDescription = description
break
}
}

const balanceColor = balance >= 0 ? cyan : red

console.log(bold('📊 FINANCIAL SUMMARY 📊'))
console.log(bold(`Total Income: ${green(`€${totalIncome}`)}`))
console.log(bold(`Total Expenses: ${red(`€${totalExpenses}`)}`))
console.log(bold(`Current Balance: ${balanceColor(`€${balance}`)}`))
console.log(
`\nLargest Expense: ${largestExpenseDescription} (${red(`€${largestExpenseAmount}`)})`
)
console.log(bold(`Total Transactions: ${transactions.length}`))
}

export {
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.

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