-
Notifications
You must be signed in to change notification settings - Fork 19
Salem Ba-Rabuod #4
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?
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,4 @@ | ||
| { | ||
| "semi": true, | ||
| "singleQuote": true | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,33 @@ | ||
| // This is the entrypoint for your application. | ||
| // node app.js | ||
| import { | ||
| addTransaction, | ||
| getLargestExpense, | ||
| printAllTransactions, | ||
| printSummary, | ||
| getTotalTransactions, | ||
| } from './finance.js'; | ||
|
|
||
| import chalk from 'chalk'; | ||
|
|
||
| console.log(chalk.bold(' PERSONAL FINANCE TRACKER \n')); | ||
|
|
||
| const transaction = { | ||
| type: 'expense', | ||
| category: 'Personal Care', | ||
| amount: 20, | ||
| description: 'baber shop', | ||
| date: '2025-01-30', | ||
| }; | ||
|
|
||
| addTransaction(transaction); | ||
|
|
||
| printAllTransactions(); | ||
| printSummary(); | ||
|
|
||
| const largestExpense = getLargestExpense(); | ||
| console.log( | ||
| chalk.bold( | ||
| `\nLargest Expense: ${largestExpense.category} (€${largestExpense.amount})`, | ||
| ), | ||
| ); | ||
|
|
||
| console.log(chalk.bold(`Total Transactions: ${getTotalTransactions()}`)); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,51 @@ | ||
| // Place here the transaction data array. Use it in your application as needed. | ||
| const transactions = []; | ||
| export const transactions = [ | ||
| { | ||
| id: 1, | ||
| type: 'income', | ||
| category: 'Salary', | ||
| amount: 3000, | ||
| description: 'Monthly salary', | ||
| date: '2025-01-02', | ||
| }, | ||
| { | ||
| id: 2, | ||
| type: 'expense', | ||
| category: 'Rent', | ||
| amount: 1200, | ||
| description: 'Monthly rent', | ||
| date: '2025-01-05', | ||
| }, | ||
| { | ||
| id: 3, | ||
| type: 'expense', | ||
| category: 'Groceries', | ||
| amount: 300, | ||
| description: 'food', | ||
| date: '2025-01-12', | ||
| }, | ||
| { | ||
| id: 4, | ||
| type: 'income', | ||
| category: 'Freelance', | ||
| amount: 500, | ||
| description: 'side-income', | ||
| date: '2025-01-15', | ||
| }, | ||
| { | ||
| id: 5, | ||
| type: 'expense', | ||
| category: 'Utilities', | ||
| amount: 150, | ||
| description: 'bills', | ||
| date: '2025-01-20', | ||
| }, | ||
| { | ||
| id: 6, | ||
| type: 'income', | ||
| category: 'Freelance', | ||
| amount: 150, | ||
| description: 'side-income', | ||
| date: '2025-01-25', | ||
| }, | ||
| ]; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,104 @@ | ||
| function addTransaction(transaction) { | ||
| // TODO: Implement this function | ||
| import { transactions } from './data.js'; | ||
| import chalk from 'chalk'; | ||
|
|
||
| export function addTransaction(transaction) { | ||
| if (!transaction.id) { | ||
| transaction.id = transactions.length + 1; | ||
| } | ||
| transactions.push(transaction); | ||
| return transactions; | ||
| } | ||
|
Comment on lines
4
to
10
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 see this function expects an array of transactions, but the assignment goal is to add one transaction at a time. What you wrote is correct, but since the function (by its name) adds a single transaction, we could simplify it to accept one transaction object instead. |
||
|
|
||
| export function getTotalIncome() { | ||
| let totalIncome = 0; | ||
| for (const transaction of transactions) { | ||
| if (transaction.type === 'income') { | ||
| totalIncome += transaction.amount; | ||
| } | ||
| } | ||
| return totalIncome; | ||
| } | ||
|
Comment on lines
+12
to
20
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. Nice work👍 |
||
|
|
||
| function getTotalIncome() { | ||
| // TODO: Implement this function | ||
| export function getTotalExpenses() { | ||
| let totalExpenses = 0; | ||
| for (const transaction of transactions) { | ||
| if (transaction.type === 'expense') { | ||
| totalExpenses += transaction.amount; | ||
| } | ||
| } | ||
| return totalExpenses; | ||
| } | ||
|
|
||
| function getTotalExpenses() { | ||
| // TODO: Implement this function | ||
| export function getBalance() { | ||
| return getTotalIncome() - getTotalExpenses(); | ||
| } | ||
|
|
||
| function getBalance() { | ||
| // TODO: Implement this function | ||
| export function getTransactionsByCategory(category) { | ||
| let filteredTransactions = []; | ||
| for (const transaction of transactions) { | ||
| if (transaction.category === category) { | ||
| filteredTransactions.push(transaction); | ||
| } | ||
| } | ||
| return filteredTransactions; | ||
| } | ||
|
|
||
| function getTransactionsByCategory(category) { | ||
| // TODO: Implement this function | ||
| export function getLargestExpense() { | ||
| let largest = null; | ||
| for (const transaction of transactions) { | ||
| if (transaction.type === 'expense') { | ||
| if (largest === null || transaction.amount > largest.amount) { | ||
| largest = transaction; | ||
| } | ||
| } | ||
| } | ||
| return largest; | ||
| } | ||
|
|
||
| function getLargestExpense() { | ||
| // TODO: Implement this function | ||
| export function printAllTransactions() { | ||
| console.log('All Transactions:'); | ||
| let id = 1; | ||
| for (const transaction of transactions) { | ||
| const { type, category, amount, description } = transaction; | ||
|
|
||
| // To determine the income or expense, and to set a proper color for display | ||
| let amountColor = null; | ||
| if (transaction.type === 'income') { | ||
| amountColor = chalk.green(`€${amount}`); | ||
| } else { | ||
| amountColor = chalk.red(`€${amount}`); | ||
| } | ||
|
|
||
| console.log( | ||
| `${id}. [${type.toUpperCase()}] ${chalk.yellow(category)} - ${amountColor} (${description})`, | ||
| ); | ||
| id++; | ||
| } | ||
|
|
||
| console.log('\n'); | ||
| } | ||
|
|
||
| function printAllTransactions() { | ||
| // TODO: Implement this function | ||
| } | ||
| export function printSummary() { | ||
| console.log(chalk.bold('FINANCIAL SUMMARY')); | ||
| console.log(chalk.green(`Total Income: €${getTotalIncome()}`)); | ||
| console.log(chalk.red(`Total Expenses: €${getTotalExpenses()}`)); | ||
| if (getBalance() >= 0) { | ||
| console.log(chalk.cyan(`Current Balance: €${getBalance()}`)); | ||
| } else { | ||
| console.log(chalk.red(`Current Balance: €${getBalance()}`)); | ||
| } | ||
| } | ||
|
|
||
| export function getTransactionsByType(type) { | ||
| let filteredTransactions = []; | ||
| for (const transaction of transactions) { | ||
| if (transaction.type === type) { | ||
| filteredTransactions.push(transaction); | ||
| } | ||
| } | ||
| return filteredTransactions; | ||
| } | ||
|
|
||
| export function getTotalTransactions() { | ||
| return transactions.length; | ||
| } | ||
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,13 @@ | ||
| { | ||
| "name": "finance-tracker", | ||
| "version": "1.0.0", | ||
| "description": "", | ||
| "main": "app.js", | ||
| "type": "module", | ||
| "scripts": { | ||
| "start": "node ./app.js" | ||
| }, | ||
| "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.
Small suggestion 🙂
Some imported functions are not used in this file.
It’s best practice to remove unused imports to keep the code clean and easier to read.