-
Notifications
You must be signed in to change notification settings - Fork 19
Mohamad Bader AA #11
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?
Mohamad Bader AA #11
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,13 @@ | ||
| # .editorconfig | ||
| root = true | ||
|
|
||
| [*] | ||
| charset = utf-8 | ||
| indent_style = space | ||
| indent_size = 2 | ||
| end_of_line = lf | ||
| insert_final_newline = true | ||
| trim_trailing_whitespace = true | ||
|
|
||
| [*.md] | ||
| trim_trailing_whitespace = false |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -156,3 +156,6 @@ dist | |
| vite.config.js.timestamp-* | ||
| vite.config.ts.timestamp-* | ||
|
|
||
| # node modules | ||
| node_modules/ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| node_modules/ | ||
| package-lock.json | ||
| dist/ | ||
| build/ | ||
| *.min.js |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "semi": true, | ||
| "trailingComma": "es5", | ||
| "singleQuote": true, | ||
| "printWidth": 80, | ||
| "tabWidth": 2, | ||
| "useTabs": false | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| // This is the entrypoint for your application. | ||
| // node app.js | ||
| const { printAllTransactions, printSummary } = require('./finance'); | ||
|
|
||
| printAllTransactions(); | ||
|
|
||
| printSummary(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,44 @@ | ||
| // 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: 'Salary', | ||
| date: '2025-01-15', | ||
| }, | ||
| { | ||
| id: 2, | ||
| type: 'expense', | ||
| category: 'housing', | ||
| amount: 1200, | ||
| description: 'Rent', | ||
| date: '2025-01-20', | ||
| }, | ||
| { | ||
| id: 3, | ||
| type: 'expense', | ||
| category: 'food', | ||
| amount: 300, | ||
| description: 'Groceries', | ||
| date: '2025-01-22', | ||
| }, | ||
| { | ||
| id: 4, | ||
| type: 'income', | ||
| category: 'side-income', | ||
| amount: 500, | ||
| description: 'Freelance', | ||
| date: '2025-01-25', | ||
| }, | ||
| { | ||
| id: 5, | ||
| type: 'expense', | ||
| category: 'bills', | ||
| amount: 150, | ||
| description: 'Utilities', | ||
| date: '2025-01-28', | ||
| }, | ||
| ]; | ||
|
|
||
| module.exports = transactions; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,87 @@ | ||
| function addTransaction(transaction) { | ||
| // TODO: Implement this function | ||
| const transactions = require('./data'); | ||
| const chalk = require('chalk'); | ||
|
|
||
| function addTransaction(newTransaction) { | ||
|
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 descriptive name for the parameter. This increases readability ⭐ |
||
| transactions.push({ ...newTransaction }); | ||
| } | ||
|
|
||
| function getTotalIncome() { | ||
| // TODO: Implement this function | ||
| let sum = 0; | ||
| for (const t of transactions) { | ||
| if (t.type === 'income') { | ||
| sum += t.amount; | ||
| } | ||
| } | ||
| return sum; | ||
| } | ||
|
|
||
| function getTotalExpenses() { | ||
| // TODO: Implement this function | ||
| let sum = 0; | ||
| for (const t of transactions) { | ||
| if (t.type === 'expense') { | ||
| sum += t.amount; | ||
| } | ||
| } | ||
| return sum; | ||
| } | ||
|
|
||
| function getBalance() { | ||
| // TODO: Implement this function | ||
| return getTotalIncome() - getTotalExpenses(); | ||
| } | ||
|
|
||
| function getTransactionsByCategory(category) { | ||
| // TODO: Implement this function | ||
| let filtered = []; | ||
| for (const t of transactions) { | ||
| if (t.category === category) { | ||
| filtered.push(t); | ||
| } | ||
| } | ||
| return filtered; | ||
| } | ||
|
|
||
| function getLargestExpense() { | ||
| // TODO: Implement this function | ||
| let largest = { amount: 0, description: 'None' }; | ||
|
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 way to make your function fail safe ⭐ |
||
| for (const t of transactions) { | ||
| if (t.type === 'expense' && t.amount > largest.amount) { | ||
| largest = t; | ||
| } | ||
| } | ||
| return largest; | ||
| } | ||
|
|
||
| function printAllTransactions() { | ||
| // TODO: Implement this function | ||
| } | ||
| console.log(chalk.bold.yellow('\n💰 PERSONAL FINANCE TRACKER 💰')); | ||
| console.log('\nAll Transactions:'); | ||
|
|
||
| transactions.forEach((t, index) => { | ||
|
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 |
||
| const { type, description, amount, category } = t; | ||
|
|
||
| const label = | ||
| type === 'income' ? chalk.green('[INCOME]') : chalk.red('[EXPENSE]'); | ||
| const categoryStyled = chalk.yellow(category); | ||
|
|
||
| console.log( | ||
| `${index + 1}. ${label} ${description} - €${amount} (${categoryStyled})` | ||
|
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. Visually it's good to display the |
||
| ); | ||
| }); | ||
| } | ||
|
|
||
| function printSummary() { | ||
| const balance = getBalance(); | ||
| const balanceColor = balance >= 0 ? chalk.cyan : chalk.red; | ||
|
|
||
| console.log(chalk.bold('\n📊 FINANCIAL SUMMARY 📊')); | ||
| console.log(`Total Income: ${chalk.green('€' + getTotalIncome())}`); | ||
| console.log(`Total Expenses: ${chalk.red('€' + getTotalExpenses())}`); | ||
| console.log(`Current Balance: ${balanceColor('€' + balance)}`); | ||
|
|
||
| const largest = getLargestExpense(); | ||
| console.log(`\nLargest Expense: ${largest.description} (€${largest.amount})`); | ||
| console.log(`Total Transactions: ${transactions.length}\n`); | ||
| } | ||
|
|
||
| module.exports = { | ||
| addTransaction, | ||
| 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,25 @@ | ||
| { | ||
| "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": "app.js", | ||
| "scripts": { | ||
| "format": "prettier --write .", | ||
| "test": "echo \"Error: no test specified\" && exit 1" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/noneeeed/c55-core-week-4.git" | ||
| }, | ||
| "keywords": [], | ||
| "author": "Bader", | ||
| "license": "ISC", | ||
| "type": "commonjs", | ||
| "bugs": { | ||
| "url": "https://github.com/noneeeed/c55-core-week-4/issues" | ||
| }, | ||
| "homepage": "https://github.com/noneeeed/c55-core-week-4#readme", | ||
| "dependencies": { | ||
| "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.
export default transactions;is more common to use thanmodule.exports = transactionsThe way you've done it is the older CommonJS Module way. The newer way is to use
exportwhich is ES Modules way.More info on the history: https://www.w3schools.com/nodejs/nodejs_modules_esm.asp