From c975ad90fedcfc338b1c647651cc2c54a37f3495 Mon Sep 17 00:00:00 2001 From: "dagimgit config --global user.email dagimvanhaileselassie@gmail.com" Date: Wed, 4 Feb 2026 20:55:35 +0100 Subject: [PATCH 1/4] feat: implement core finance tracking logic reporting --- .prettierrc | 1 + finance-tracker/app.js | 39 ++++++++++++++++- finance-tracker/data.js | 45 +++++++++++++++++++- finance-tracker/finance.js | 58 +++++++++++++++++++++---- package-lock.json | 86 ++++++++++++++++++++++++++++++++++++++ package.json | 24 +++++++++++ 6 files changed, 242 insertions(+), 11 deletions(-) create mode 100644 .prettierrc create mode 100644 package-lock.json create mode 100644 package.json diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..868c1e4 --- /dev/null +++ b/.prettierrc @@ -0,0 +1 @@ +{ "semi": true, "singleQuote": true } diff --git a/finance-tracker/app.js b/finance-tracker/app.js index 7cfcd07..74fc81f 100644 --- a/finance-tracker/app.js +++ b/finance-tracker/app.js @@ -1,3 +1,38 @@ -// This is the entrypoint for your application. -// node app.js +const chalk = require('chalk'); +const { + getTotalIncome, + getTotalExpenses, + getBalance, + getLargestExpense, + printAllTransactions, +} = require('./finance'); +// HEADER +console.log(chalk.bold('\nšŸ’° PERSONAL FINANCE TRACKER šŸ’°')); + +// ALL TRANSACTIONS +console.log('\nAll Transactions:'); +printAllTransactions(); + +// SUMMARY HEADER +console.log(chalk.bold('\nšŸ“Š FINANCIAL SUMMARY šŸ“Š')); + +const income = getTotalIncome(); +const expenses = getTotalExpenses(); +const balance = getBalance(); + +console.log(`Total Income: ${chalk.green('€' + income)}`); +console.log(`Total Expenses: ${chalk.red('€' + expenses)}`); + +// Balance color logic +const balanceColor = balance >= 0 ? chalk.cyan : chalk.red; +console.log(`Current Balance: ${balanceColor('€' + balance)}`); + +// LARGEST EXPENSE & COUNT +const largest = getLargestExpense(); +console.log( + `\nLargest Expense: ${chalk.yellow(largest.description)} (${chalk.red('€' + largest.amount)})`, +); +// Use the .length property of the transactions array we imported +const transactions = require('./data'); +console.log(`Total Transactions: ${transactions.length}\n`); diff --git a/finance-tracker/data.js b/finance-tracker/data.js index d7863ff..8f44322 100644 --- a/finance-tracker/data.js +++ b/finance-tracker/data.js @@ -1,2 +1,45 @@ // Place here the transaction data array. Use it in your application as needed. -const transactions = []; \ No newline at end of file +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-16', + }, + { + id: 3, + type: 'expense', + category: 'food', + amount: 300, + description: 'Groceries', + date: '2025-01-17', + }, + { + id: 4, + type: 'income', + category: 'side-income', + amount: 500, + description: 'Freelance', + date: '2025-01-18', + }, + { + id: 5, + type: 'expense', + category: 'bills', + amount: 150, + description: 'Utilities', + date: '2025-01-19', + }, +]; + +module.exports = transactions; diff --git a/finance-tracker/finance.js b/finance-tracker/finance.js index ac2118f..c2625f2 100644 --- a/finance-tracker/finance.js +++ b/finance-tracker/finance.js @@ -1,27 +1,69 @@ +const transactions = require('./data'); + function addTransaction(transaction) { - // TODO: Implement this function + return [...transactions, transaction]; } function getTotalIncome() { - // TODO: Implement this function + let total = 0; + for (const tx of transactions) { + if (tx.type === 'income') { + total += tx.amount; + } + } + return total; } function getTotalExpenses() { - // TODO: Implement this function + let total = 0; + for (const tx of transactions) { + if (tx.type === 'expense') { + total += tx.amount; + } + } + return total; } function getBalance() { - // TODO: Implement this function + return getTotalIncome() - getTotalExpenses(); } function getTransactionsByCategory(category) { - // TODO: Implement this function + const filtered = []; + for (const tx of transactions) { + if (tx.category === category) { + filtered.push(tx); + } + } + return filtered; } function getLargestExpense() { - // TODO: Implement this function + let largest = { amount: 0, description: 'None' }; + + for (const tx of transactions) { + // Check if it is an expense AND if it's larger than our current record + if (tx.type === 'expense' && tx.amount > largest.amount) { + largest = tx; + } + } + return largest; } function printAllTransactions() { - // TODO: Implement this function -} \ No newline at end of file + transactions.forEach((tx, index) => { + console.log( + `${index + 1}. [${tx.type.toUpperCase()}] ${tx.description} - €${tx.amount} (${tx.category})`, + ); + }); +} + +module.exports = { + addTransaction, + getTotalIncome, + getTotalExpenses, + getBalance, + getTransactionsByCategory, + getLargestExpense, + printAllTransactions, +}; diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..52239c6 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,86 @@ +{ + "name": "c55-core-week-4", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "c55-core-week-4", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "chalk": "^4.1.2" + } + }, + "node_modules/ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "license": "MIT", + "dependencies": { + "color-convert": "^2.0.1" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/chalk/ansi-styles?sponsor=1" + } + }, + "node_modules/chalk": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", + "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "license": "MIT", + "dependencies": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, + "node_modules/color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "license": "MIT", + "dependencies": { + "color-name": "~1.1.4" + }, + "engines": { + "node": ">=7.0.0" + } + }, + "node_modules/color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", + "license": "MIT" + }, + "node_modules/has-flag": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", + "license": "MIT", + "engines": { + "node": ">=8" + } + }, + "node_modules/supports-color": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", + "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "license": "MIT", + "dependencies": { + "has-flag": "^4.0.0" + }, + "engines": { + "node": ">=8" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..956b005 --- /dev/null +++ b/package.json @@ -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/Dagim02/c55-core-week-4.git" + }, + "keywords": [], + "author": "", + "license": "ISC", + "type": "commonjs", + "bugs": { + "url": "https://github.com/Dagim02/c55-core-week-4/issues" + }, + "homepage": "https://github.com/Dagim02/c55-core-week-4#readme", + "dependencies": { + "chalk": "^4.1.2" + } +} From e40724b6efde570b51946ff1b68f6d277e4b52b2 Mon Sep 17 00:00:00 2001 From: "Dagim H." Date: Wed, 4 Feb 2026 21:03:54 +0100 Subject: [PATCH 2/4] Update data.js --- finance-tracker/data.js | 1 - 1 file changed, 1 deletion(-) diff --git a/finance-tracker/data.js b/finance-tracker/data.js index 8f44322..24b1087 100644 --- a/finance-tracker/data.js +++ b/finance-tracker/data.js @@ -1,4 +1,3 @@ -// Place here the transaction data array. Use it in your application as needed. const transactions = [ { id: 1, From 4feeffa9dfa4d422b95fafd7833329476110086a Mon Sep 17 00:00:00 2001 From: Dagim02 Date: Mon, 2 Mar 2026 10:31:45 +0100 Subject: [PATCH 3/4] update: correctionOnWeek-4 --- .prettierrc | 6 +++++- finance-tracker/app.js | 9 +++++---- finance-tracker/finance.js | 40 ++++++++++++++++---------------------- package-lock.json | 19 ++++++++++++++++++ package.json | 3 +++ 5 files changed, 49 insertions(+), 28 deletions(-) diff --git a/.prettierrc b/.prettierrc index 868c1e4..1d296fb 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1 +1,5 @@ -{ "semi": true, "singleQuote": true } +{ + "semi": true, + "singleQuote": true, + "tabWidth": 2 +} diff --git a/finance-tracker/app.js b/finance-tracker/app.js index 74fc81f..f566196 100644 --- a/finance-tracker/app.js +++ b/finance-tracker/app.js @@ -1,4 +1,6 @@ const chalk = require('chalk'); +const transactions = require('./data'); + const { getTotalIncome, getTotalExpenses, @@ -28,11 +30,10 @@ console.log(`Total Expenses: ${chalk.red('€' + expenses)}`); const balanceColor = balance >= 0 ? chalk.cyan : chalk.red; console.log(`Current Balance: ${balanceColor('€' + balance)}`); -// LARGEST EXPENSE & COUNT +// largest expenses and total const largest = getLargestExpense(); console.log( - `\nLargest Expense: ${chalk.yellow(largest.description)} (${chalk.red('€' + largest.amount)})`, + `\nLargerst Expenses: ${chalk.yellow(largest.description)} (${chalk.red('€' + largest.amount)})`, ); -// Use the .length property of the transactions array we imported -const transactions = require('./data'); + console.log(`Total Transactions: ${transactions.length}\n`); diff --git a/finance-tracker/finance.js b/finance-tracker/finance.js index c2625f2..89a53fc 100644 --- a/finance-tracker/finance.js +++ b/finance-tracker/finance.js @@ -5,22 +5,19 @@ function addTransaction(transaction) { } function getTotalIncome() { - let total = 0; - for (const tx of transactions) { - if (tx.type === 'income') { - total += tx.amount; - } - } + const total = transactions.reduce( + (total, tx) => (tx.type === 'income' ? total + tx.amount : total), + 0, + ); + return total; } function getTotalExpenses() { - let total = 0; - for (const tx of transactions) { - if (tx.type === 'expense') { - total += tx.amount; - } - } + const total = transactions.reduce( + (total, tx) => (tx.type === 'expense' ? total + tx.amount : total), + 0, + ); return total; } @@ -29,24 +26,21 @@ function getBalance() { } function getTransactionsByCategory(category) { - const filtered = []; - for (const tx of transactions) { - if (tx.category === category) { - filtered.push(tx); - } - } - return filtered; + const getByCatagory = transactions.filter((tx) => tx.category === category); + return getByCatagory; } function getLargestExpense() { - let largest = { amount: 0, description: 'None' }; - + let largest = null; for (const tx of transactions) { - // Check if it is an expense AND if it's larger than our current record - if (tx.type === 'expense' && tx.amount > largest.amount) { + if ( + tx.type === 'expense' && + (largest === null || tx.amount > largest.amount) + ) { largest = tx; } } + return largest; } diff --git a/package-lock.json b/package-lock.json index 52239c6..228d484 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,9 @@ "license": "ISC", "dependencies": { "chalk": "^4.1.2" + }, + "devDependencies": { + "prettier": "^3.8.1" } }, "node_modules/ansi-styles": { @@ -70,6 +73,22 @@ "node": ">=8" } }, + "node_modules/prettier": { + "version": "3.8.1", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.8.1.tgz", + "integrity": "sha512-UOnG6LftzbdaHZcKoPFtOcCKztrQ57WkHDeRD9t/PTQtmT0NHSeWWepj6pS0z/N7+08BHFDQVUrfmfMRcZwbMg==", + "dev": true, + "license": "MIT", + "bin": { + "prettier": "bin/prettier.cjs" + }, + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/prettier/prettier?sponsor=1" + } + }, "node_modules/supports-color": { "version": "7.2.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", diff --git a/package.json b/package.json index 956b005..9e16bf2 100644 --- a/package.json +++ b/package.json @@ -20,5 +20,8 @@ "homepage": "https://github.com/Dagim02/c55-core-week-4#readme", "dependencies": { "chalk": "^4.1.2" + }, + "devDependencies": { + "prettier": "^3.8.1" } } From 55c49da71a604693fd7f6d99c9a9ddd8a47818d8 Mon Sep 17 00:00:00 2001 From: Dagim02 Date: Mon, 2 Mar 2026 11:36:52 +0100 Subject: [PATCH 4/4] update:UsingShorterMethod-prettier --- .prettierrc | 6 +++--- finance-tracker/app.js | 6 +++--- finance-tracker/finance.js | 16 ++++++---------- 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/.prettierrc b/.prettierrc index 1d296fb..b423197 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,5 +1,5 @@ { - "semi": true, - "singleQuote": true, - "tabWidth": 2 + "semi": true, + "singleQuote": true, + "tabWidth": 2 } diff --git a/finance-tracker/app.js b/finance-tracker/app.js index f566196..d62b218 100644 --- a/finance-tracker/app.js +++ b/finance-tracker/app.js @@ -9,14 +9,14 @@ const { printAllTransactions, } = require('./finance'); -// HEADER +// Header console.log(chalk.bold('\nšŸ’° PERSONAL FINANCE TRACKER šŸ’°')); -// ALL TRANSACTIONS +// All Transactions console.log('\nAll Transactions:'); printAllTransactions(); -// SUMMARY HEADER +// Summary Header console.log(chalk.bold('\nšŸ“Š FINANCIAL SUMMARY šŸ“Š')); const income = getTotalIncome(); diff --git a/finance-tracker/finance.js b/finance-tracker/finance.js index 89a53fc..ed81656 100644 --- a/finance-tracker/finance.js +++ b/finance-tracker/finance.js @@ -31,16 +31,12 @@ function getTransactionsByCategory(category) { } function getLargestExpense() { - let largest = null; - for (const tx of transactions) { - if ( - tx.type === 'expense' && - (largest === null || tx.amount > largest.amount) - ) { - largest = tx; - } - } - + const largest = transactions + .filter((tx) => tx.type === 'expense') + .reduce( + (max, tx) => (max === null || tx.amount > max.amount ? tx : max), + null, + ); return largest; }