generated from HackYourFuture/core-assignment-week-4
-
Notifications
You must be signed in to change notification settings - Fork 19
Monerh A #14
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
Open
Miuroro
wants to merge
2
commits into
HackYourAssignment:main
Choose a base branch
from
Miuroro:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Monerh A #14
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| node_modules/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "semi": true, | ||
| "singleQuote": true, | ||
| "tabWidth": 2, | ||
| "trailingComma": "es5", | ||
| "printWidth": 80 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,21 @@ | ||
| // This is the entrypoint for your application. | ||
| // node app.js | ||
| import { transactions } from './data.js'; | ||
| import { | ||
| addTransaction, | ||
| getTotalIncome, | ||
| getTotalExpenses, | ||
| getBalance, | ||
| getTransactionsByCategory, | ||
| getLargestExpense, | ||
| printAllTransactions, | ||
| printSummary, | ||
| getAverageExpensePerCategory, | ||
| } from './finance.js'; | ||
|
|
||
| // Main execution | ||
| console.clear(); | ||
| printAllTransactions(transactions); | ||
| printSummary(transactions); | ||
| getAverageExpensePerCategory(transactions); | ||
| // End of file | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,83 @@ | ||
| // 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-11-25', | ||
| }, | ||
| { | ||
| id: 2, | ||
| type: 'expense', | ||
| category: 'groceries', | ||
| amount: 150.5, | ||
| description: 'Supermarket shopping', | ||
| date: '2025-12-05', | ||
| }, | ||
| { | ||
| id: 3, | ||
| type: 'expense', | ||
| category: 'rent', | ||
| amount: 980, | ||
| description: 'Monthly rent', | ||
| date: '2025-12-01', | ||
| }, | ||
| { | ||
| id: 4, | ||
| type: 'income', | ||
| category: 'freelance', | ||
| amount: 420, | ||
| description: 'Selling socks', | ||
| date: '2025-12-20', | ||
| }, | ||
| { | ||
| id: 5, | ||
| type: 'expense', | ||
| category: 'utilities', | ||
| amount: 110.25, | ||
| description: 'Electricity and water', | ||
| date: '2025-12-15', | ||
| }, | ||
| { | ||
| id: 6, | ||
| type: 'expense', | ||
| category: 'entertainment', | ||
| amount: 60, | ||
| description: 'Concert tickets', | ||
| date: '2025-12-27', | ||
| }, | ||
| { | ||
| id: 7, | ||
| type: 'income', | ||
| category: 'investment', | ||
| amount: 150, | ||
| description: 'Stock market', | ||
| date: '2025-12-10', | ||
| }, | ||
| { | ||
| id: 8, | ||
| type: 'expense', | ||
| category: 'transportation', | ||
| amount: 45, | ||
| description: 'Monthly bus pass', | ||
| date: '2025-11-03', | ||
| }, | ||
| { | ||
| id: 9, | ||
| type: 'expense', | ||
| category: 'groceries', | ||
| amount: 95, | ||
| description: 'Weekly groceries', | ||
| date: '2025-11-10', | ||
| }, | ||
| { | ||
| id: 10, | ||
| type: 'expense', | ||
| category: 'entertainment', | ||
| amount: 120, | ||
| description: 'New video game', | ||
| date: '2025-11-20', | ||
| }, | ||
| ]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,143 @@ | ||
| function addTransaction(transaction) { | ||
| // TODO: Implement this function | ||
| import chalk from 'chalk'; | ||
| import { transactions } from './data.js'; | ||
|
|
||
| // Adds a new transaction to the transactions array | ||
| export function addTransaction(transaction) { | ||
| transactions.push(...[transaction]); | ||
| } | ||
|
|
||
| // Calculates and returns the total sum of all income transactions | ||
| export function getTotalIncome() { | ||
| let total = 0; | ||
| for (const transaction of transactions) { | ||
| if (transaction.type === 'income') { | ||
| total += transaction.amount; | ||
| } | ||
| } | ||
| return total; | ||
| } | ||
|
|
||
| function getTotalIncome() { | ||
| // TODO: Implement this function | ||
| // Calculates and returns the total sum of all expense transactions | ||
| export function getTotalExpenses() { | ||
| let total = 0; | ||
| for (const transaction of transactions) { | ||
| if (transaction.type === 'expense') { | ||
| total += transaction.amount; | ||
| } | ||
| } | ||
| return total; | ||
| } | ||
|
|
||
| function getTotalExpenses() { | ||
| // TODO: Implement this function | ||
| // Calculates and returns the balance (total income minus total expenses) | ||
| export function getBalance() { | ||
| return getTotalIncome() - getTotalExpenses(); | ||
| } | ||
|
|
||
| function getBalance() { | ||
| // TODO: Implement this function | ||
| // Filters and returns all transactions that match a specific category | ||
| export function getTransactionsByCategory(transactions, category) { | ||
| const result = []; | ||
| for (const transaction of transactions) { | ||
| if (transaction.category === category) { | ||
| result.push(transaction); | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| function getTransactionsByCategory(category) { | ||
| // TODO: Implement this function | ||
| // Finds and returns the transaction object with the highest expense amount | ||
| export function getLargestExpense() { | ||
| let largestExpense = null; | ||
| let maxAmount = 0; | ||
| for (const transaction of transactions) { | ||
| if (transaction.type === 'expense' && transaction.amount > maxAmount) { | ||
| maxAmount = transaction.amount; | ||
| largestExpense = transaction; | ||
| } | ||
| } | ||
| return largestExpense; | ||
| } | ||
|
|
||
| function getLargestExpense() { | ||
| // TODO: Implement this function | ||
| // Displays all transactions in a formatted console output | ||
| export function printAllTransactions(transactions) { | ||
| console.log(chalk.bold.cyan('\n💰 PERSONAL FINANCE TRACKER 💰\n')); | ||
| console.log(chalk.bold('All Transactions:\n')); | ||
|
|
||
| transactions.forEach((transaction, index) => { | ||
| // destructuring | ||
| const { id, type, category, amount, description } = transaction; | ||
| const typeLabel = type === 'income' ? '[INCOME]' : '[EXPENSE]'; // createing type label | ||
| const coloredAmount = | ||
| type === 'income' ? chalk.green(`€${amount}`) : chalk.red(`€${amount}`); // green for income red for expense | ||
| const coloredCategory = chalk.yellow(`(${category})`); // yellow for category | ||
|
|
||
| console.log( | ||
| `${index + 1}. ${typeLabel} ${description} - ${coloredAmount} ${coloredCategory}` | ||
| ); // output: 1. typeLabel description - €amount (category) | ||
| }); | ||
| console.log(); // extra line for better readability | ||
| } | ||
|
|
||
| function printAllTransactions() { | ||
| // TODO: Implement this function | ||
| // Print summary of financial data | ||
| export function printSummary(transactions) { | ||
| const totalIncome = getTotalIncome(transactions); | ||
| const totalExpenses = getTotalExpenses(transactions); | ||
| const balance = getBalance(transactions); | ||
| const largestExpense = getLargestExpense(transactions); | ||
| const transactionCount = transactions.length; | ||
|
|
||
| console.log(chalk.bold.cyan('\n📊 FINANCIAL SUMMARY 📊\n')); | ||
|
|
||
| console.log(`Total Income: ${chalk.green(`€${totalIncome}`)}`); | ||
| console.log(`Total Expenses: ${chalk.red(`€${totalExpenses}`)}`); | ||
|
|
||
| // Balance color-coded based on positive or negative | ||
| 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`); | ||
| } | ||
|
|
||
| // Calculates and returns the average expense amount for each category | ||
| export function getAverageExpensePerCategory(transactions) { | ||
|
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. Nice implementation 👍. |
||
| const expenses = transactions.filter( | ||
| (transaction) => transaction.type === 'expense' | ||
| ); | ||
| const categoryTotals = {}; | ||
| const categoryCounts = {}; | ||
| /** | ||
| * This loop iterates through each transaction in the `expenses` array. | ||
| * For each transaction, it updates two objects: | ||
| * 1. `categoryTotals`: It adds the transaction's `amount` to a running total for its specific `category`. | ||
| * If a category is seen for the first time, it's initialized to 0 before adding the amount. | ||
| * 2. `categoryCounts`: It increments a counter for the transaction's `category`. | ||
| * If a category is seen for the first time, its count is initialized to 0 before incrementing. | ||
| * This effectively calculates both the total amount spent and the number of transactions per category. | ||
| */ | ||
| for (const { category, amount } of expenses) { | ||
| categoryTotals[category] = (categoryTotals[category] || 0) + amount; | ||
| categoryCounts[category] = (categoryCounts[category] || 0) + 1; | ||
| } | ||
|
|
||
| const averages = {}; // storing average expense per category | ||
| for (const category in categoryTotals) { | ||
| const total = categoryTotals[category]; | ||
| const count = categoryCounts[category]; | ||
| averages[category] = +(total / count).toFixed(2); // Calculate average and round to 2 decimal places | ||
| } | ||
|
|
||
| console.log(chalk.bold('Average Expense per Category:')); | ||
| for (const category in averages) { | ||
| const average = averages[category]; | ||
| console.log(`- ${category}: ${chalk.red(`€${average}`)}`); | ||
| } | ||
|
|
||
| return averages; | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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.