-
Notifications
You must be signed in to change notification settings - Fork 18
Dagim H. #17
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?
Dagim H. #17
Changes from 1 commit
c975ad9
e40724b
4feeffa
9ecf21d
f53fd3b
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 @@ | ||
| { "semi": true, "singleQuote": true } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,38 @@ | ||
| // This is the entrypoint for your application. | ||
| // node app.js | ||
| const chalk = require('chalk'); | ||
| const { | ||
| getTotalIncome, | ||
| getTotalExpenses, | ||
| getBalance, | ||
| getLargestExpense, | ||
| printAllTransactions, | ||
| } = require('./finance'); | ||
|
|
||
| // HEADER | ||
| console.log(chalk.bold('\n💰 PERSONAL FINANCE TRACKER 💰')); | ||
|
|
||
| // ALL TRANSACTIONS | ||
| console.log('\nAll Transactions:'); | ||
| printAllTransactions(); | ||
|
|
||
| // SUMMARY HEADER | ||
| console.log(chalk.bold('\n📊 FINANCIAL SUMMARY 📊')); | ||
|
|
||
| const income = getTotalIncome(); | ||
| const expenses = getTotalExpenses(); | ||
| const balance = getBalance(); | ||
|
|
||
| console.log(`Total Income: ${chalk.green('€' + income)}`); | ||
| console.log(`Total Expenses: ${chalk.red('€' + expenses)}`); | ||
|
|
||
| // Balance color logic | ||
| const balanceColor = balance >= 0 ? chalk.cyan : chalk.red; | ||
| console.log(`Current Balance: ${balanceColor('€' + balance)}`); | ||
|
|
||
| // LARGEST EXPENSE & COUNT | ||
| const largest = getLargestExpense(); | ||
| console.log( | ||
| `\nLargest Expense: ${chalk.yellow(largest.description)} (${chalk.red('€' + largest.amount)})`, | ||
| ); | ||
| // Use the .length property of the transactions array we imported | ||
| const transactions = require('./data'); | ||
|
||
| console.log(`Total Transactions: ${transactions.length}\n`); | ||
| 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: 'housing', | ||
| amount: 1200, | ||
| description: 'Rent', | ||
| date: '2025-01-16', | ||
| }, | ||
| { | ||
| id: 3, | ||
| type: 'expense', | ||
| category: 'food', | ||
| amount: 300, | ||
| description: 'Groceries', | ||
| date: '2025-01-17', | ||
| }, | ||
| { | ||
| id: 4, | ||
| type: 'income', | ||
| category: 'side-income', | ||
| amount: 500, | ||
| description: 'Freelance', | ||
| date: '2025-01-18', | ||
| }, | ||
| { | ||
| id: 5, | ||
| type: 'expense', | ||
| category: 'bills', | ||
| amount: 150, | ||
| description: 'Utilities', | ||
| date: '2025-01-19', | ||
| }, | ||
| ]; | ||
|
|
||
| module.exports = transactions; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,69 @@ | ||
| const transactions = require('./data'); | ||
|
|
||
| function addTransaction(transaction) { | ||
| // TODO: Implement this function | ||
| return [...transactions, transaction]; | ||
| } | ||
|
|
||
| function getTotalIncome() { | ||
| // TODO: Implement this function | ||
| let total = 0; | ||
| for (const tx of transactions) { | ||
|
||
| if (tx.type === 'income') { | ||
| total += tx.amount; | ||
| } | ||
| } | ||
| return total; | ||
| } | ||
|
|
||
| function getTotalExpenses() { | ||
| // TODO: Implement this function | ||
| let total = 0; | ||
|
||
| for (const tx of transactions) { | ||
| if (tx.type === 'expense') { | ||
| total += tx.amount; | ||
| } | ||
| } | ||
| return total; | ||
| } | ||
|
|
||
| function getBalance() { | ||
| // TODO: Implement this function | ||
| return getTotalIncome() - getTotalExpenses(); | ||
| } | ||
|
|
||
| function getTransactionsByCategory(category) { | ||
| // TODO: Implement this function | ||
| const filtered = []; | ||
|
||
| for (const tx of transactions) { | ||
| if (tx.category === category) { | ||
| filtered.push(tx); | ||
| } | ||
| } | ||
| return filtered; | ||
| } | ||
|
|
||
| function getLargestExpense() { | ||
| // TODO: Implement this function | ||
| let largest = { amount: 0, description: 'None' }; | ||
|
|
||
| for (const tx of transactions) { | ||
| // Check if it is an expense AND if it's larger than our current record | ||
| if (tx.type === 'expense' && tx.amount > largest.amount) { | ||
| largest = tx; | ||
|
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. this works but it is a bit confusing... why do you first declare let largest = { amount: 0, description: 'None' }; and in the loop you overwrite largest and have it point to tx;? What do we need { amount: 0, description: 'None' }; Defining largest = { amount: 0, description: 'None' }; makes me expect that aftr the function largest will be { amount: 1200, description: 'rent' }. Either define largest as null (like Shadi did) or even better, keep largest but make sure that largest.amount and largest description point to the greatest expense after running the function
Author
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. Thank you for the feedback. I will take the correction regarding the object initialization and format the entire codebase with Prettier. I will resubmit it as soon as possible. |
||
| } | ||
| } | ||
| return largest; | ||
| } | ||
|
|
||
| function printAllTransactions() { | ||
| // TODO: Implement this function | ||
| } | ||
| transactions.forEach((tx, index) => { | ||
| console.log( | ||
| `${index + 1}. [${tx.type.toUpperCase()}] ${tx.description} - €${tx.amount} (${tx.category})`, | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| module.exports = { | ||
| addTransaction, | ||
| getTotalIncome, | ||
| getTotalExpenses, | ||
| getBalance, | ||
| getTransactionsByCategory, | ||
| getLargestExpense, | ||
| printAllTransactions, | ||
| }; | ||
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,24 @@ | ||
| { | ||
| "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/Dagim02/c55-core-week-4.git" | ||
| }, | ||
| "keywords": [], | ||
| "author": "", | ||
| "license": "ISC", | ||
| "type": "commonjs", | ||
| "bugs": { | ||
| "url": "https://github.com/Dagim02/c55-core-week-4/issues" | ||
| }, | ||
| "homepage": "https://github.com/Dagim02/c55-core-week-4#readme", | ||
| "dependencies": { | ||
|
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. I'm missing prettier...Format entire codebase with Prettier before submission |
||
| "chalk": "^4.1.2" | ||
| } | ||
| } | ||
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.
in backticks you just can use Return, you don't have to use \n. So
console.log(
\nLargest Expense: ${chalk.yellow(largest.description)} (${chalk.red('€' + largest.amount)})becomes
console.log(
Largest Expense: ${chalk.yellow(largest.description)} (${chalk.red('€' + largest.amount)})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.
Thank you for the detailed feedback. I realize there were several areas for improvement, specifically regarding object initialization, using built-in array methods like .filter(), removing unnecessary \n escape characters in template literals, and overall code formatting.
I am reviewing all the lessons now and will refactor the code to be more concise and professional. I will resubmit the corrected version with Prettier formatting applied very soon.