-
Notifications
You must be signed in to change notification settings - Fork 19
Jawad A. #15
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?
Jawad A. #15
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 @@ | ||
| 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,14 @@ | ||
| // This is the entrypoint for your application. | ||
| // node app.js | ||
| import { transactions } from './data.js'; | ||
| import { | ||
| addTransaction, | ||
| getTotalIncome, | ||
| getTotalExpenses, | ||
| getBalance, | ||
| getTransactionsByCategory, | ||
| getLargestExpense, | ||
| printAllTransactions, | ||
| printSummary, | ||
| } from './finance.js'; | ||
|
Comment on lines
+2
to
+11
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. Small suggestion 🙂 Some imported functions are not used in this file. |
||
|
|
||
| printAllTransactions(transactions); | ||
| printSummary(transactions); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,84 @@ | ||
| // 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-12-28', | ||
| }, | ||
| { | ||
| id: 2, | ||
| type: 'expense', | ||
| category: 'rent', | ||
| amount: 800, | ||
| description: 'housing', | ||
| date: '2026-01-01', | ||
| }, | ||
| { | ||
| id: 3, | ||
| type: 'expense', | ||
| category: 'utility', | ||
| amount: 250, | ||
| description: 'bills', | ||
| date: '2026-01-16', | ||
| }, | ||
| { | ||
| id: 4, | ||
| type: 'expense', | ||
| category: 'groceries', | ||
| amount: 180, | ||
| description: 'food', | ||
| date: '2026-01-26', | ||
| }, | ||
| { | ||
| id: 5, | ||
| type: 'income', | ||
| category: 'benefits', | ||
| amount: 200, | ||
| description: 'huurtoeslag', | ||
| date: '2026-01-20', | ||
| }, | ||
| { | ||
| id: 6, | ||
| type: 'expense', | ||
| category: 'insurance', | ||
| amount: 210, | ||
| description: 'bills', | ||
| date: '2026-01-24', | ||
| }, | ||
| { | ||
| id: 7, | ||
| type: 'income', | ||
| category: 'salary', | ||
| amount: 350, | ||
| description: 'freelance', | ||
| date: '2026-01-19', | ||
| }, | ||
| { | ||
| id: 8, | ||
| type: 'expense', | ||
| category: 'transport', | ||
| amount: 80, | ||
| description: 'bills', | ||
| date: '2026-01-30', | ||
| }, | ||
| { | ||
| id: 9, | ||
| type: 'expense', | ||
| category: 'entertainment', | ||
| amount: 60, | ||
| description: 'pc-games', | ||
| date: '2026-01-01', | ||
| }, | ||
| { | ||
| id: 10, | ||
| type: 'expense', | ||
| category: '', | ||
| amount: 200, | ||
| description: '', | ||
| date: '2026-01-20', | ||
| }, | ||
| ]; | ||
|
|
||
| export default transactions; | ||
|
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.
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,85 @@ | ||
| function addTransaction(transaction) { | ||
| // TODO: Implement this function | ||
| import transactions from `./data.js`; | ||
| import chalk from 'chalk'; | ||
|
|
||
| export function addTransaction(transaction) { | ||
| transactions.push({ ...transaction }); | ||
| } | ||
|
|
||
| function getTotalIncome() { | ||
| // TODO: Implement this function | ||
| export function getTotalIncome(transactions) { | ||
| let totalIncome = 0; | ||
| for (const transaction of transactions) { | ||
| if (transaction.type === `income`) { | ||
| totalIncome += transaction.amount; | ||
| } | ||
| } | ||
| return totalIncome; | ||
| } | ||
|
|
||
| function getTotalExpenses() { | ||
| // TODO: Implement this function | ||
| export function getTotalExpenses(transactions) { | ||
| let totalExpenses = 0; | ||
| for (const transaction of transactions) { | ||
| if (transaction.type === `expense`) { | ||
| totalExpenses += transaction.amount; | ||
| } | ||
| } | ||
| return totalExpenses; | ||
| } | ||
|
|
||
| function getBalance() { | ||
| // TODO: Implement this function | ||
| export function getBalance() { | ||
| return getTotalIncome() - getTotalExpenses(); | ||
| } | ||
|
Comment on lines
+28
to
30
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. There is a logic issue in
|
||
|
|
||
| function getTransactionsByCategory(category) { | ||
| // TODO: Implement this function | ||
| export function getTransactionsByCategory(transactions, category) { | ||
| const result = []; | ||
| for (const transaction of transactions) { | ||
| if (transaction.category === category) { | ||
| result.push(transaction); | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| function getLargestExpense() { | ||
| // TODO: Implement this function | ||
|
|
||
| export function getLargestExpense() { | ||
| let largest = null; | ||
| for (const transaction of transactions) { | ||
| if (transaction.type === 'expense' && (!largest || transaction.amount > largest.amount)) { | ||
| largest = transaction; | ||
| } | ||
| } | ||
| return largest; | ||
| } | ||
|
|
||
| function printAllTransactions() { | ||
| // TODO: Implement this function | ||
| export function printAllTransactions(transactions) { | ||
| console.log(chalk.bold.cyan('PERSONAL FINANCE TRACKER\n')); | ||
| console.log(chalk.bold('All Transactions:\n')); | ||
|
|
||
| transactions.forEach((transaction, index) => { | ||
| const { id, type, category, amount, description } = transaction; | ||
| const typeLabel = type === 'income' ? '[INCOME]' : '[EXPENSE]'; | ||
| const coloredAmount = | ||
| type === 'income' ? chalk.green(`€${amount}`) : chalk.red(`€${amount}`); | ||
| const coloredCategory = chalk.yellow(`(${category})`); | ||
|
|
||
| console.log(`${index + 1}. ${typeLabel} ${description} - ${coloredAmount} ${coloredCategory}`); | ||
| }); | ||
|
|
||
| export function printSummary(transactions) { | ||
| const totalIncome = getTotalIncome(transactions); | ||
| const totalExpenses = getTotalExpenses(transactions); | ||
| const balance = getBalance(transactions); | ||
|
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. Because |
||
| const largestExpense = getLargestExpense(transactions); | ||
| const transactionCount = transactions.length; | ||
|
|
||
| console.log(chalk.bold.cyan('FINANCIAL SUMMARY\n')); | ||
| console.log(`Total Income: ${chalk.green(`€${totalIncome}`)}`); | ||
| console.log(`Total Expenses: ${chalk.red(`€${totalExpenses}`)}`); | ||
| const balanceColor = balance >= 0 ? chalk.cyan : chalk.red; | ||
| console.log(`Current Balance: ${balanceColor(`€${balance}`)}`); | ||
|
|
||
| if (largestExpense) { | ||
| console.log( | ||
| `\nLargest Expense: ${largestExpense.description} (${chalk.red(`€${largestExpense.amount}`)})`); | ||
| } | ||
| console.log(`Total Transactions: ${chalk.bold(transactionCount)}\n`); | ||
| } | ||
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,16 @@ | ||
| { | ||
| "name": "hyf-assignment", | ||
| "version": "1.0.0", | ||
| "description": "", | ||
| "main": "index.js", | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1" | ||
| }, | ||
| "keywords": [], | ||
| "author": "", | ||
| "license": "ISC", | ||
| "type": "commonjs", | ||
|
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. You mix two module styles: CommonJS and ES modules. If you use Or you can enable ES modules ( For example, in |
||
| "dependencies": { | ||
| "chalk": "^5.6.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.
This import here will not work because of the module styles:
CommonJS. Check my comment on thepackage.jsonfile.