generated from HackYourFuture/core-assignment-week-4
-
Notifications
You must be signed in to change notification settings - Fork 19
Pavel T #20
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
pavel-tisner
wants to merge
1
commit into
HackYourAssignment:main
Choose a base branch
from
pavel-tisner: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
Pavel T #20
Changes from all commits
Commits
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
File renamed without changes.
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,15 @@ | ||
| // This is the entrypoint for your application. | ||
| // node app.js | ||
| import { transactions } from './data.js'; | ||
| import { addTransaction, printSummary } from './finance.js'; | ||
|
|
||
| const initFn = (transactions) => { | ||
| addTransaction(transactions, { | ||
| id: 6, | ||
| type: 'income', | ||
| category: 'salary', | ||
| amount: 3000, | ||
| description: 'Monthly salary', | ||
| date: '2025-02-15', | ||
| }); | ||
| return printSummary(transactions); | ||
| }; | ||
| console.log(initFn(transactions)); | ||
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,42 @@ | ||
| // 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-01-15', | ||
| }, | ||
| { | ||
| id: 2, | ||
| type: 'expense', | ||
| category: 'housing', | ||
| amount: 1200, | ||
| description: 'Rent', | ||
| date: '2025-01-15', | ||
| }, | ||
| { | ||
| id: 3, | ||
| type: 'expense', | ||
| category: 'food', | ||
| amount: 300, | ||
| description: 'Groceries', | ||
| date: '2025-01-15', | ||
| }, | ||
| { | ||
| id: 4, | ||
| type: 'income', | ||
| category: 'side-income', | ||
| amount: 500, | ||
| description: 'Freelance', | ||
| date: '2025-01-15', | ||
| }, | ||
| { | ||
| id: 5, | ||
| type: 'expense', | ||
| category: 'bills', | ||
| amount: 150, | ||
| description: 'Utilities', | ||
| date: '2025-01-15', | ||
| }, | ||
| ]; |
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,120 @@ | ||
| function addTransaction(transaction) { | ||
| // TODO: Implement this function | ||
| import chalk from 'chalk'; | ||
|
|
||
| // Return a new array to avoid mutating the original transactions list | ||
| function addTransaction(transactions, transaction) { | ||
| return [...transactions, transaction]; | ||
| } | ||
|
|
||
| function getTotalIncome(transactions) { | ||
| let total = 0; | ||
| for (const transaction of transactions) { | ||
| const { type, amount } = transaction; | ||
| if (type === 'income') { | ||
| total += amount; | ||
| } | ||
| } | ||
| return total; | ||
| } | ||
|
|
||
| function getTotalExpenses(transactions) { | ||
| let total = 0; | ||
| for (const transaction of transactions) { | ||
| const { type, amount } = transaction; | ||
| if (type === 'expense') { | ||
| total += amount; | ||
| } | ||
| } | ||
| return total; | ||
| } | ||
|
|
||
| function getBalance(transactions) { | ||
| const totalIncome = getTotalIncome(transactions); | ||
| const totalExpenses = getTotalExpenses(transactions); | ||
| return totalIncome - totalExpenses; | ||
| } | ||
|
|
||
| function getTotalIncome() { | ||
| // TODO: Implement this function | ||
| function getTransactionsByCategory(transactions, category) { | ||
| const result = []; | ||
| for (const transaction of transactions) { | ||
| if (transaction.category === category) { | ||
| result.push(transaction); | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| function getTotalExpenses() { | ||
| // TODO: Implement this function | ||
| function getLargestExpense(transactions) { | ||
| let largestExpense = 0; | ||
| for (const transaction of transactions) { | ||
| const { type, amount } = transaction; | ||
| if (type === 'expense' && amount > largestExpense) { | ||
| largestExpense = amount; | ||
| } | ||
| } | ||
| return largestExpense; | ||
| } | ||
|
|
||
| function getBalance() { | ||
| // TODO: Implement this function | ||
| // Uses getLargestExpense, so the array is iterated more than once | ||
| function getLargestExpenseDescription(transactions) { | ||
| let largestExpenseDescription = ''; | ||
| for (const transaction of transactions) { | ||
| const { type, amount, description } = transaction; | ||
| if (type === 'expense' && amount === getLargestExpense(transactions)) { | ||
| largestExpenseDescription = description; | ||
| } | ||
| } | ||
| return largestExpenseDescription; | ||
| } | ||
|
|
||
| function getTransactionsByCategory(category) { | ||
| // TODO: Implement this function | ||
| function printAllTransactions(transactions) { | ||
| let result = ''; | ||
| for (const transaction of transactions) { | ||
| const { id, type, category, amount, description } = transaction; | ||
| const coloredCategory = chalk.yellow(category); | ||
| const coloredAmount = | ||
| type === 'income' ? chalk.green(amount) : chalk.red(amount); | ||
| result += `${id}. [${type.toUpperCase()}] ${coloredCategory[0].toUpperCase() + coloredCategory.slice(1)} - €${coloredAmount} (${description.toLowerCase()}) | ||
| `; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| function getLargestExpense() { | ||
| // TODO: Implement this function | ||
| // Calculations are delegated to helper functions; this function handles formatting only | ||
| function printSummary(transactions) { | ||
| const totalIncome = chalk.bold.green(getTotalIncome(transactions)); | ||
| const totalExpenses = chalk.bold.red(getTotalExpenses(transactions)); | ||
| const balance = | ||
| getBalance(transactions) >= 0 | ||
| ? chalk.bold.cyan(getBalance(transactions)) | ||
| : chalk.bold.red(getBalance(transactions)); | ||
| const largestExpense = chalk.red(getLargestExpense(transactions)); | ||
| const largestExpenseDescription = getLargestExpenseDescription(transactions); | ||
| const AllTransactions = printAllTransactions(transactions); | ||
| const transactionCount = transactions.length; | ||
| return ` | ||
| 💰 PERSONAL FINANCE TRACKER 💰 | ||
|
|
||
| All Transactions: | ||
| ${AllTransactions} | ||
|
|
||
| 📊 FINANCIAL SUMMARY 📊 | ||
| Total Income: €${totalIncome} | ||
| Total Expenses: €${totalExpenses} | ||
| Current Balance: €${balance} | ||
|
|
||
| Largest Expense: ${largestExpenseDescription} (${largestExpense}) | ||
| Total Transactions: ${transactionCount} | ||
| `; | ||
| } | ||
|
|
||
| function printAllTransactions() { | ||
| // TODO: Implement this function | ||
| } | ||
| export { | ||
| addTransaction, | ||
| getTotalIncome, | ||
| getTotalExpenses, | ||
| getBalance, | ||
| getTransactionsByCategory, | ||
| getLargestExpense, | ||
| getLargestExpenseDescription, | ||
| printAllTransactions, | ||
| printSummary, | ||
| }; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,27 @@ | ||
| { | ||
| "name": "finance-tracker", | ||
| "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": "app.js", | ||
| "scripts": { | ||
| "start": "node app.js", | ||
| "format": "prettier --write ." | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/pavel-tisner/c55-core-week-4.git" | ||
| }, | ||
| "author": "", | ||
| "license": "ISC", | ||
| "bugs": { | ||
| "url": "https://github.com/pavel-tisner/c55-core-week-4/issues" | ||
| }, | ||
| "homepage": "https://github.com/pavel-tisner/c55-core-week-4#readme", | ||
| "dependencies": { | ||
| "chalk": "^5.6.2" | ||
| }, | ||
| "devDependencies": { | ||
| "prettier": "^3.8.1" | ||
| }, | ||
| "type": "module" | ||
| } |
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.
Good design choice using an immutable
addTransaction👍However, the returned array is not used in
app.js, so the new transaction is not included in the summary. There are solutions for that; you could consider them.