-
Notifications
You must be signed in to change notification settings - Fork 19
Baraah A. #2
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?
Baraah A. #2
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 |
|---|---|---|
|
|
@@ -156,3 +156,5 @@ dist | |
| vite.config.js.timestamp-* | ||
| vite.config.ts.timestamp-* | ||
|
|
||
| node_modules/ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "semi"; true, | ||
| "singleQuote"; true, | ||
| "tabWidth"; 2, | ||
| "trailingComma"; "es5", | ||
| "printWidth"; 80 | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,11 @@ | ||
| // This is the entrypoint for your application. | ||
| // node app.js | ||
|
|
||
| const chalk = require('chalk'); | ||
| const { printAllTransactions, printSummary } = require('./finance'); | ||
|
|
||
| console.log(chalk.bold('💰 PERSONAL FINANCE TRACKER 💰')); | ||
|
|
||
| printAllTransactions(); | ||
| printSummary(); | ||
|
|
| 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-05' | ||
| }, | ||
| { | ||
| id: 3, | ||
| type: 'expense', | ||
| category: 'food', | ||
| amount: 300, | ||
| description: 'Groceries', | ||
| date: '2025-01-10' | ||
| }, | ||
| { | ||
| id: 4, | ||
| type: 'income', | ||
| category: 'side-income', | ||
| amount: 500, | ||
| description: 'Freelance work', | ||
| date: '2025-01-20' | ||
| }, | ||
| { | ||
| id: 5, | ||
| type: 'expense', | ||
| category: 'bills', | ||
| amount: 150, | ||
| description: 'Utilities', | ||
| date: '2025-01-18' | ||
| }, | ||
| ]; | ||
|
|
||
| module.exports = transactions; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,111 @@ | ||
| const chalk = require('chalk'); | ||
| const transactions = require('./data'); | ||
|
|
||
|
|
||
| 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. |
||
| transaction.push({...transaction}); // | ||
| } | ||
|
|
||
| function getTotalIncome() { | ||
| // TODO: Implement this function | ||
| let totalIncome=0; | ||
| for(const transaction of transactions){ | ||
| if (transaction.type === 'income'){ | ||
| totalIncome += transaction.amount; | ||
| } | ||
| } | ||
| return totalIncome; | ||
| } | ||
|
|
||
| function getTotalExpenses() { | ||
| // TODO: Implement this function | ||
| let totalExpenses=0; | ||
| for(const transaction of transactions){ | ||
| if (transaction.type === 'expense'){ | ||
| totalExpenses += transaction.amount; | ||
| } | ||
| } | ||
| return totalExpenses; | ||
| } | ||
|
|
||
| function getBalance() { | ||
| // TODO: Implement this function | ||
| return getTotalIncome() - getTotalExpenses(); | ||
| } | ||
|
|
||
| function getTransactionsByCategory(category) { | ||
| // TODO: Implement this function | ||
| const result =[]; | ||
| for (const transaction of transactions){ | ||
| if (transaction.category === category){ | ||
| result.push(transaction); | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| function getLargestExpense() { | ||
| // TODO: Implement this function | ||
| let largest = null; | ||
| for(const transaction of transactions) | ||
| if(transaction.type === 'expense'){ | ||
| if(largest=== null || transaction.amount > largest.amount){ | ||
| largest= transaction; | ||
| } | ||
| } | ||
| return largest; | ||
| } | ||
|
|
||
| function printAllTransactions() { | ||
| // TODO: Implement this function | ||
| } | ||
|
|
||
| console.log(chalk.bold('All transactions')); | ||
|
|
||
| for(const transaction of transactions){ | ||
| const amountColor = transaction.type === 'income' | ||
|
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. One of the requirements is to use object destructuring. How you've implemented it works perfectly fine. This is just an opportunity to try it out and learn more about it. More info: https://javascript.info/destructuring-assignment#object-destructuring |
||
| ? chalk.green(`€${transaction.amount}`) | ||
| : chalk.red(`€${transaction.amount}`); | ||
|
|
||
| console.log( | ||
| `${transaction.id}. [${ | ||
| transaction.type.toUpperCase() | ||
| }] ${transaction.description} - ${amountColor} (${chalk.yellow( | ||
| transaction.category | ||
| )})` | ||
| ); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| function printSummary(){ | ||
| const totalIncome = getTotalIncome(); | ||
|
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. Formatting: The code inside this function should be indented. |
||
| const totalExpenses = getTotalExpenses(); | ||
| const balance = getBalance (); | ||
| const numberTransaction = transactions.length; | ||
| const largestExpense = getLargestExpense(); | ||
|
|
||
| console.log(chalk.bold('\n📊 FINANCIAL SUMMARY 📊')); | ||
| console.log(chalk.bold(`Total Income: €${totalIncome}`)); | ||
| console.log(chalk.bold(`Total Expenses: €${totalExpenses}`)); | ||
|
|
||
| const balanceColor = | ||
| balance >= 0 ? chalk.cyan(`€${balance}`) : chalk.red(`€${balance}`); | ||
|
|
||
| console.log(chalk.bold(`Current Balance: ${balanceColor}`)); | ||
| console.log(chalk.bold(`Total Transactions: ${transactions.length}`)); | ||
|
|
||
| if (largestExpense) { | ||
| console.log( | ||
| chalk.bold( | ||
| `Largest Expense: ${largestExpense.description} (€${largestExpense.amount})` | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| module.exports = { printAllTransactions, printSummary }; | ||
| //console.log(chalk.bold('💰 PERSONAL FINANCE TRACKER 💰')); | ||
|
|
||
| //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,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/thebaraah/c55-core-week-4.git" | ||
| }, | ||
| "keywords": [], | ||
| "author": "", | ||
| "license": "ISC", | ||
| "type": "commonjs", | ||
| "bugs": { | ||
| "url": "https://github.com/thebaraah/c55-core-week-4/issues" | ||
| }, | ||
| "homepage": "https://github.com/thebaraah/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