-
Notifications
You must be signed in to change notification settings - Fork 19
Yana P #8
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?
Yana P #8
Changes from 1 commit
5c86a7a
e64a0db
18de204
6185a53
4bef681
3d93d31
149859f
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,7 @@ | ||
| { | ||
| "semi": true, | ||
| "singleQuote": true, | ||
| "trailingComma": "es5", | ||
| "printWidth": 80, | ||
| "tabWidth": 2 | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,40 @@ | ||
| // This is the entrypoint for your application. | ||
| // node app.js | ||
| import { | ||
| addTransaction, | ||
| getTotalIncome, | ||
| getTotalExpenses, | ||
| getBalance, | ||
| getTransactionsByCategory, | ||
| getLargestExpense, | ||
| printAllTransactions, | ||
| printSummary, | ||
| } from './finance.js'; | ||
| import transactions from './data.js'; | ||
|
|
||
| addTransaction( | ||
| 'expense', | ||
| 'groceries', | ||
| 98, | ||
| 'Weekly supermarket shopping', | ||
| '2025-01-25' | ||
| ); | ||
|
|
||
| const totalIncome = getTotalIncome(transactions); | ||
| const totalExpenses = getTotalExpenses(transactions); | ||
| const balance = getBalance(transactions); | ||
| const transactionsByCategory = getTransactionsByCategory( | ||
| transactions, | ||
| 'groceries' | ||
| ); | ||
| const largestExpenseAmount = getLargestExpense(transactions); | ||
|
|
||
| console.log(transactions); | ||
| console.log(totalIncome); | ||
| console.log(totalExpenses); | ||
| console.log(balance); | ||
| console.log(transactionsByCategory); | ||
| console.log(largestExpenseAmount); | ||
|
|
||
| printAllTransactions(transactions); | ||
| console.log(printSummary(transactions)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,77 @@ | ||
| // 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: 120, | ||
| description: 'Weekly supermarket shopping', | ||
| date: '2025-01-16', | ||
| }, | ||
| { | ||
| id: 3, | ||
| type: 'expense', | ||
| category: 'transport', | ||
| amount: 45, | ||
| description: 'Monthly public transport pass', | ||
| date: '2025-01-17', | ||
| }, | ||
| { | ||
| id: 4, | ||
| type: 'income', | ||
| category: 'freelance', | ||
| amount: 600, | ||
| description: 'Freelance web project', | ||
| date: '2025-01-20', | ||
| }, | ||
| { | ||
| id: 5, | ||
| type: 'expense', | ||
| category: 'entertainment', | ||
| amount: 75, | ||
| description: 'Concert tickets', | ||
| date: '2025-01-21', | ||
| }, | ||
| { | ||
| id: 6, | ||
| type: 'expense', | ||
| category: 'utilities', | ||
| amount: 150, | ||
| description: 'Electricity and water bill', | ||
| date: '2025-01-22', | ||
| }, | ||
| { | ||
| id: 7, | ||
| type: 'income', | ||
| category: 'gift', | ||
| amount: 200, | ||
| description: 'Birthday gift from family', | ||
| date: '2025-01-23', | ||
| }, | ||
| { | ||
| id: 8, | ||
| type: 'expense', | ||
| category: 'health', | ||
| amount: 90, | ||
| description: 'Doctor appointment', | ||
| date: '2025-01-24', | ||
| }, | ||
| { | ||
| id: 9, | ||
| type: 'expense', | ||
| category: 'education', | ||
| amount: 300, | ||
| description: 'Online course fee', | ||
| date: '2025-01-25', | ||
| }, | ||
| ]; | ||
|
|
||
| export default transactions; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,118 @@ | ||
| function addTransaction(transaction) { | ||
| // TODO: Implement this function | ||
| import transactions from './data.js'; | ||
| import chalk from 'chalk'; | ||
|
|
||
| export function addTransaction(type, category, amount, description, date) { | ||
| const newTransaction = { | ||
| id: transactions.length + 1, | ||
| type, | ||
| category, | ||
| amount, | ||
| description, | ||
| date, | ||
| }; | ||
| transactions.push(newTransaction); | ||
| return newTransaction; | ||
| } | ||
|
|
||
| export function getTotalIncome(transactions) { | ||
| let totalIncome = 0; | ||
| for (const transaction of transactions) { | ||
| if (transaction.type === 'income') { | ||
| totalIncome += transaction.amount; | ||
| } | ||
| } | ||
| return totalIncome; | ||
| } | ||
|
|
||
| export function getTotalExpenses(transactions) { | ||
| let totalExpenses = 0; | ||
| for (const transaction of transactions) { | ||
| if (transaction.type === 'expense') { | ||
| totalExpenses += transaction.amount; | ||
| } | ||
| } | ||
| return totalExpenses; | ||
| } | ||
|
|
||
| function getTotalIncome() { | ||
| // TODO: Implement this function | ||
| export function getBalance(transactions) { | ||
| const totalIncome = getTotalIncome(transactions); | ||
| const totalExpenses = getTotalExpenses(transactions); | ||
| const balance = totalIncome - totalExpenses; | ||
|
|
||
| return balance; | ||
| } | ||
|
|
||
| function getTotalExpenses() { | ||
| // TODO: Implement this function | ||
| export function getTransactionsByCategory(transactions, category) { | ||
| let transactionByCategory = []; | ||
|
|
||
| for (const transaction of transactions) { | ||
| if (transaction.category === category) { | ||
| transactionByCategory.push(transaction); | ||
| } | ||
| } | ||
|
|
||
| return transactionByCategory; | ||
| } | ||
|
|
||
| function getBalance() { | ||
| // TODO: Implement this function | ||
| export function getLargestExpense(transactions) { | ||
| const expenseTransactions = transactions.filter( | ||
| (transaction) => transaction.type === 'expense' | ||
| ); | ||
|
|
||
| let largest = expenseTransactions[0].amount; | ||
|
|
||
| for (const transaction of expenseTransactions) { | ||
| if (transaction.amount > largest) { | ||
| largest = transaction.amount; | ||
| } | ||
| } | ||
| return largest; | ||
| } | ||
|
|
||
| function getTransactionsByCategory(category) { | ||
| // TODO: Implement this function | ||
| export function printAllTransactions(transactions) { | ||
| console.log(chalk.bold('All Transactions:')); | ||
| for (const transaction of transactions) { | ||
| const { id, type, category, amount, description } = transaction; | ||
|
|
||
| const typeFormat = | ||
| type === 'income' | ||
| ? chalk.green(type.toUpperCase()) | ||
| : chalk.red(type.toUpperCase()); | ||
|
|
||
| const categoryFormat = getFirstCharacterToUp(chalk.yellow(category)); | ||
|
|
||
| const amountFormat = chalk.bold(amount); | ||
|
|
||
| const descriptionFormat = description.toLowerCase(); | ||
|
|
||
| const formattingTransaction = `${id}. [${typeFormat}] ${categoryFormat} - €${amountFormat} (${descriptionFormat})`; | ||
| console.log(formattingTransaction); | ||
| } | ||
| } | ||
|
|
||
| export function printSummary(transactions) { | ||
| const totalIncome = chalk.green(getTotalIncome(transactions)); | ||
| const totalExpenses = chalk.red(getTotalExpenses(transactions)); | ||
| const balance = | ||
| getBalance(transactions) >= 0 | ||
| ? chalk.cyan(getBalance(transactions)) | ||
| : chalk.red(getBalance(transactions)); | ||
| const numOfTransactions = chalk.bold(transactions.length); | ||
| const largestExpense = getLargestExpense(transactions); | ||
| const largestExpenseForm = chalk.bold(largestExpense.amount); | ||
| const largestExpenseDescr = getFirstCharacterToUp(largestExpense.description); | ||
|
|
||
| const summary = `📊 FINANCIAL SUMMARY 📊 \nTotal Income: €${totalIncome}\nTotal Expenses: €${totalExpenses}\nCurrent Balance: €${balance}\n\nLargest Expense: ${largestExpenseDescr} (${largestExpenseForm})\nTotal Transactions: ${numOfTransactions}`; | ||
|
|
||
| return summary; | ||
| } | ||
|
|
||
| function getLargestExpense() { | ||
| // TODO: Implement this function | ||
| function getFirstCharacterToUp(word) { | ||
| return word[0].toUpperCase() + word.slice(1); | ||
| } | ||
|
|
||
| function printAllTransactions() { | ||
| // TODO: Implement this function | ||
| } | ||
| // if (balance >= 0) { | ||
| // return chalk.bold.cyan(balance); | ||
| // } else { | ||
| // return chalk.bold.red(balance); | ||
| // } |
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,17 @@ | ||
| { | ||
| "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", | ||
| "start": "node finance-tracker/app.js" | ||
| }, | ||
| "type": "module", | ||
| "keywords": [], | ||
| "author": "", | ||
| "license": "ISC", | ||
| "dependencies": { | ||
| "chalk": "^4.1.2" | ||
|
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. no prettier for code formatting?
Author
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 had only a .prettierrc file before, but Prettier wasn’t installed as a project dependency. |
||
| } | ||
| } | ||
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.
nice touch to add a script for starting the app