-
Notifications
You must be signed in to change notification settings - Fork 19
Atiqa N. #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Atiqa N. #19
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "trailingComma": "es5", | ||
| "tabWidth": 4, | ||
| "semi": false, | ||
| "singleQuote": true | ||
| } |
| 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) | ||
|
|
||
| // Display all transactions | ||
| printAllTransactions() | ||
|
|
||
| // Display financial summary | ||
| printSummary() | ||
| 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 |
| 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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" | ||
| } | ||
| } |
There was a problem hiding this comment.
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 ⭐