-
Notifications
You must be signed in to change notification settings - Fork 19
Mareh A. #18
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?
Mareh A. #18
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 @@ | ||
| { | ||
| "semi": true, | ||
| "singleQuote": true, | ||
| "tabWidth": 2, | ||
| "trailingComma": "none" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,2 @@ | ||
| // This is the entrypoint for your application. | ||
| // node app.js | ||
|
|
| 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: 1000, | ||
| description: 'Weekly groceries shopping', | ||
| date: '2025-01-16' | ||
| }, | ||
| { | ||
| id: 3, | ||
| type: 'expense', | ||
| category: 'rent', | ||
| amount: 1500, | ||
| description: 'Monthly rent payment', | ||
| date: '2025-01-30' | ||
| }, | ||
| { | ||
| id: 4, | ||
| type: 'income', | ||
| category: 'freelance', | ||
| amount: 500, | ||
| description: 'Freelance project payment', | ||
| date: '2025-01-20' | ||
| }, | ||
| { | ||
| id: 5, | ||
| type: 'expense', | ||
| category: 'entertainment', | ||
| amount: 75, | ||
| description: 'Movie tickets', | ||
| date: '2025-01-18' | ||
| } | ||
| ]; | ||
|
|
||
| module.exports = transactions; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,155 @@ | ||
| /***2. Required Functions** | ||
|
|
||
| Implement these functions using appropriate loops and array methods: | ||
|
|
||
| - `addTransaction(transaction)` - Add new transaction to array | ||
| - `getTotalIncome()` - Sum all income using a loop | ||
| - `getTotalExpenses()` - Sum all expenses using a loop | ||
| - `getBalance()` - Calculate total income minus expenses | ||
| - `getTransactionsByCategory(category)` - Filter transactions (use loop + push or indexOf/includes) | ||
| - `getLargestExpense()` - Find highest expense amount | ||
| - `printAllTransactions()` - Display all transactions with formatting*/ | ||
|
|
||
| const transactions = require('./data.js'); | ||
| const chalk = require('chalk'); | ||
| function addTransaction(transaction) { | ||
| // TODO: Implement this function | ||
|
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. Code clean up: Remove these TODO statements. Now that you have implemented the code, these are not relevant anymore. |
||
| const newTransaction = { | ||
| ...transaction, | ||
| id: transactions.length + 1 | ||
| }; | ||
| transactions.push(newTransaction); | ||
| } | ||
|
|
||
| function getTotalIncome() { | ||
| // TODO: Implement this function | ||
| let total = 0; | ||
| for (let i = 0; i < transactions.length; i++) { | ||
|
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 Learn more: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of |
||
| if (transactions[i].type === 'income') { | ||
| //total = transactions[i].amount + total; | ||
| total += transactions[i].amount; | ||
| } | ||
| } | ||
| return total; | ||
| } | ||
|
|
||
| function getTotalExpenses() { | ||
| // TODO: Implement this function | ||
| let total = 0; | ||
| //for(const transaction of transactions) | ||
| for (let i = 0; i < transactions.length; i++) { | ||
|
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.
|
||
| if (transactions[i].type === 'expense') { | ||
| total = transactions[i].amount + total; | ||
| } | ||
| } | ||
| return total; | ||
| } | ||
|
|
||
| function getBalance() { | ||
| // TODO: Implement this function | ||
| const Balance = getTotalIncome() - getTotalExpenses(); | ||
| return Balance; | ||
| } | ||
|
|
||
| function getTransactionsByCategory(category) { | ||
| // TODO: Implement this function | ||
| const result = []; | ||
| for (let i = 0; i < transactions.length; i++) { | ||
|
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.
|
||
| if (transactions[i].category === category) { | ||
| result.push(transactions[i]); | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| function getLargestExpense() { | ||
| // TODO: Implement this function | ||
| let largest = null; | ||
|
|
||
| for (let i = 0; i < transactions.length; i++) { | ||
| if (transactions[i].type === 'expense') { | ||
| if (largest === null || transactions[i].amount > largest.amount) { | ||
| largest = transactions[i]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return largest; | ||
| } | ||
|
|
||
| function printAllTransactions() { | ||
| // TODO: Implement this function | ||
| } | ||
| console.log(chalk.cyan.bold('\n 💰 PERSONAL FINANCE TRACKER 💰\n')); | ||
|
|
||
| for (let i = 0; i < transactions.length; i++) { | ||
| const transaction = transactions[i]; | ||
| const { id, type, description, amount, category } = transaction; | ||
|
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 ⭐ |
||
| const typeDisplay = | ||
| type === 'income' | ||
| ? chalk.green.bold('[income]') | ||
| : chalk.red.bold('[expense]'); | ||
|
|
||
| const amountDisplay = | ||
| type === 'income' ? chalk.green(`€${amount}`) : chalk.red(`€${amount}`); | ||
|
|
||
| console.log( | ||
| `${chalk.yellow(id)}. ${typeDisplay} ${description} - ${amountDisplay} ` + | ||
| `(${chalk.yellow(category)})` | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| function printSummary() { | ||
| const totalIncome = getTotalIncome(); | ||
| const totalExpenses = getTotalExpenses(); | ||
| const balance = getBalance(); | ||
| const largestExpense = getLargestExpense(); | ||
| const transactionCount = transactions.length; | ||
|
|
||
| console.log(chalk.cyan.bold('\n📊 FINANCIAL SUMMARY 📊\n')); | ||
|
|
||
| console.log(chalk.bold(`Total Income: ${chalk.green(`€${totalIncome}`)}`)); | ||
|
|
||
| console.log(chalk.bold(`Total Expenses: ${chalk.red(`€${totalExpenses}`)}`)); | ||
|
|
||
| const balanceColor = balance >= 0 ? chalk.cyan : chalk.red; | ||
| console.log(chalk.bold(`Current Balance: ${balanceColor(`€${balance}`)}`)); | ||
|
|
||
| if (largestExpense) { | ||
| console.log( | ||
| chalk.bold( | ||
| `Largest Expense: ${largestExpense.description} (` + | ||
| `${chalk.red(`€${largestExpense.amount}`)})` | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| console.log(chalk.bold(`Total Transactions : ${transactionCount}`)); | ||
| } | ||
|
|
||
| function removeTransaction(id) { | ||
|
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. Well done on implementing a bonus challenge ⭐ |
||
| let indexToRemove = -1; | ||
| let i = 0; | ||
|
|
||
| while (i < transactions.length) { | ||
| if (transactions[i].id === id) { | ||
| indexToRemove = i; | ||
| break; | ||
| } | ||
| i++; | ||
| } | ||
|
|
||
| if (indexToRemove !== -1) { | ||
| return transactions.splice(indexToRemove, 1)[0]; | ||
| } | ||
| return null; | ||
| } | ||
| module.exports = { | ||
| addTransaction, | ||
| getTotalIncome, | ||
| getTotalExpenses, | ||
| getBalance, | ||
| getTransactionsByCategory, | ||
| getLargestExpense, | ||
| printAllTransactions, | ||
| printSummary, | ||
| removeTransaction | ||
| }; | ||
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,27 @@ | ||
| { | ||
| "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/mareh-aboghanem/c55-core-week-4.git" | ||
| }, | ||
| "keywords": [], | ||
| "author": "", | ||
| "license": "ISC", | ||
| "type": "commonjs", | ||
| "bugs": { | ||
| "url": "https://github.com/mareh-aboghanem/c55-core-week-4/issues" | ||
| }, | ||
| "homepage": "https://github.com/mareh-aboghanem/c55-core-week-4#readme", | ||
| "dependencies": { | ||
| "chalk": "^4.1.2" | ||
| }, | ||
| "devDependencies": { | ||
| "prettier": "^3.8.1" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| const { printAllTransactions, printSummary } = require('./finance.js'); | ||
|
|
||
| printAllTransactions(); | ||
|
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. Although this is fine to use |
||
| printSummary(); | ||
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