From ff21725fb53382ef485d98229baf8b3bb5aee63f Mon Sep 17 00:00:00 2001 From: Salem Ba-rabuod Date: Tue, 3 Feb 2026 10:53:18 +0100 Subject: [PATCH 1/3] Creating files and writing some functions --- .prettierrc | 4 +++ finance-tracker/app.js | 16 ++++++++++++ finance-tracker/data.js | 51 +++++++++++++++++++++++++++++++++++++- finance-tracker/finance.js | 48 ++++++++++++++++++++++++++--------- package-lock.json | 27 ++++++++++++++++++++ package.json | 13 ++++++++++ 6 files changed, 146 insertions(+), 13 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..1efd31a --- /dev/null +++ b/.prettierrc @@ -0,0 +1,4 @@ +{ + "semi": true, + "singleQuote": true +} diff --git a/finance-tracker/app.js b/finance-tracker/app.js index 7cfcd07..73a9e76 100644 --- a/finance-tracker/app.js +++ b/finance-tracker/app.js @@ -1,3 +1,19 @@ // This is the entrypoint for your application. // node app.js +import { + addTransaction, + getTotalIncome, + getTotalExpenses, + getBalance, + getTransactionsByCategory, + getLargestExpense, + printAllTransactions, + printSummary, +} from './finance.js'; + +console.log(`Total Income: ${getTotalIncome()}`); +console.log(`Total Expenses: ${getTotalExpenses()}`); +console.log(`Current Balance: ${getBalance()}`); + +console.log(getTransactionsByCategory('freelance')); diff --git a/finance-tracker/data.js b/finance-tracker/data.js index d7863ff..ecc43f2 100644 --- a/finance-tracker/data.js +++ b/finance-tracker/data.js @@ -1,2 +1,51 @@ // Place here the transaction data array. Use it in your application as needed. -const transactions = []; \ No newline at end of file +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', + }, +]; diff --git a/finance-tracker/finance.js b/finance-tracker/finance.js index ac2118f..5e97d49 100644 --- a/finance-tracker/finance.js +++ b/finance-tracker/finance.js @@ -1,27 +1,51 @@ -function addTransaction(transaction) { +import { transactions } from './data.js'; + +export function addTransaction(transaction) { // TODO: Implement this function } -function getTotalIncome() { - // TODO: Implement this function +export function getTotalIncome() { + 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() { + 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(transactions) - getTotalExpenses(transactions); } -function getTransactionsByCategory(category) { - // TODO: Implement this function +export function getTransactionsByCategory(category) { + let result = []; + for (const transaction of transactions) { + if (transaction.category === category) { + result.push(transaction); + } + } + return result; } -function getLargestExpense() { +export function getLargestExpense() { // TODO: Implement this function } -function printAllTransactions() { +export function printAllTransactions() { // TODO: Implement this function -} \ No newline at end of file +} + +export function printSummary() { + +} diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..148aad2 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,27 @@ +{ + "name": "finance-tracker", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "finance-tracker", + "version": "1.0.0", + "dependencies": { + "chalk": "^5.6.2" + } + }, + "node_modules/chalk": { + "version": "5.6.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-5.6.2.tgz", + "integrity": "sha512-7NzBL0rN6fMUW+f7A6Io4h40qQlG+xGmtMxfbnH/K7TAtt8JQWVQK+6g0UXKMeVJoyV5EkkNsErQ8pVD3bLHbA==", + "license": "MIT", + "engines": { + "node": "^12.17.0 || ^14.13 || >=16.0.0" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..a2e2a7a --- /dev/null +++ b/package.json @@ -0,0 +1,13 @@ +{ + "name": "finance-tracker", + "version": "1.0.0", + "description": "", + "main": "app.js", + "type": "module", + "scripts": { + "start": "node ./finance-tracker/app.js" + }, + "dependencies": { + "chalk": "^5.6.2" + } +} From ec67375582c4a205179db7c7ecbb7b12557006b5 Mon Sep 17 00:00:00 2001 From: Salem Ba-rabuod Date: Tue, 3 Feb 2026 20:58:24 +0100 Subject: [PATCH 2/3] solved the assignment --- finance-tracker/app.js | 35 +++++++++++++++---- finance-tracker/data.js | 12 +++---- finance-tracker/finance.js | 71 +++++++++++++++++++++++++++++++++----- 3 files changed, 97 insertions(+), 21 deletions(-) diff --git a/finance-tracker/app.js b/finance-tracker/app.js index 73a9e76..23715a7 100644 --- a/finance-tracker/app.js +++ b/finance-tracker/app.js @@ -1,6 +1,3 @@ -// This is the entrypoint for your application. -// node app.js - import { addTransaction, getTotalIncome, @@ -10,10 +7,34 @@ import { getLargestExpense, printAllTransactions, printSummary, + getTransactionsByType, + getTotalTransactions, } from './finance.js'; -console.log(`Total Income: ${getTotalIncome()}`); -console.log(`Total Expenses: ${getTotalExpenses()}`); -console.log(`Current Balance: ${getBalance()}`); +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[0].category} (€${largestExpense[0].amount})`, + ), +); -console.log(getTransactionsByCategory('freelance')); +console.log(chalk.bold(`Total Transactions: ${getTotalTransactions()}`)); diff --git a/finance-tracker/data.js b/finance-tracker/data.js index ecc43f2..94fac0b 100644 --- a/finance-tracker/data.js +++ b/finance-tracker/data.js @@ -3,7 +3,7 @@ export const transactions = [ { id: 1, type: 'income', - category: 'salary', + category: 'Salary', amount: 3000, description: 'Monthly salary', date: '2025-01-02', @@ -11,7 +11,7 @@ export const transactions = [ { id: 2, type: 'expense', - category: 'rent', + category: 'Rent', amount: 1200, description: 'Monthly rent', date: '2025-01-05', @@ -19,7 +19,7 @@ export const transactions = [ { id: 3, type: 'expense', - category: 'groceries', + category: 'Groceries', amount: 300, description: 'food', date: '2025-01-12', @@ -27,7 +27,7 @@ export const transactions = [ { id: 4, type: 'income', - category: 'freelance', + category: 'Freelance', amount: 500, description: 'side-income', date: '2025-01-15', @@ -35,7 +35,7 @@ export const transactions = [ { id: 5, type: 'expense', - category: 'utilities', + category: 'Utilities', amount: 150, description: 'bills', date: '2025-01-20', @@ -43,7 +43,7 @@ export const transactions = [ { id: 6, type: 'income', - category: 'freelance', + category: 'Freelance', amount: 150, description: 'side-income', date: '2025-01-25', diff --git a/finance-tracker/finance.js b/finance-tracker/finance.js index 5e97d49..a83a01e 100644 --- a/finance-tracker/finance.js +++ b/finance-tracker/finance.js @@ -1,7 +1,12 @@ import { transactions } from './data.js'; +import chalk from 'chalk'; export function addTransaction(transaction) { - // TODO: Implement this function + if (!transaction[0].id) { + transaction[0].id = transactions.length + 1; + } + transactions.push(...transaction); + return transactions; } export function getTotalIncome() { @@ -25,27 +30,77 @@ export function getTotalExpenses() { } export function getBalance() { - return getTotalIncome(transactions) - getTotalExpenses(transactions); + return getTotalIncome() - getTotalExpenses(); } export function getTransactionsByCategory(category) { - let result = []; + let filteredTransactions = []; for (const transaction of transactions) { if (transaction.category === category) { - result.push(transaction); + filteredTransactions.push(transaction); } } - return result; + return filteredTransactions; } export function getLargestExpense() { - // TODO: Implement this function + const filteredTransactions = getTransactionsByType('expense'); + let largestTransaction = []; + let largestExpense = null; + for (const filteredTransaction of filteredTransactions) { + if (filteredTransaction.amount > largestExpense) { + largestExpense = filteredTransaction.amount; + largestTransaction.pop(filteredTransaction); + largestTransaction.push(filteredTransaction); + } + } + return largestTransaction; } export function printAllTransactions() { - // TODO: Implement this function + 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'); } 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; } From 04b8481360f73c53428077f165f0e1193cce7ee9 Mon Sep 17 00:00:00 2001 From: Salem Ba-rabuod Date: Tue, 17 Feb 2026 10:43:45 +0100 Subject: [PATCH 3/3] Applied mentor feedback --- finance-tracker/.gitignore | 1 + .prettierrc => finance-tracker/.prettierrc | 0 finance-tracker/app.js | 23 +++++++------------ finance-tracker/finance.js | 22 ++++++++---------- .../package-lock.json | 0 package.json => finance-tracker/package.json | 2 +- 6 files changed, 20 insertions(+), 28 deletions(-) create mode 100644 finance-tracker/.gitignore rename .prettierrc => finance-tracker/.prettierrc (100%) rename package-lock.json => finance-tracker/package-lock.json (100%) rename package.json => finance-tracker/package.json (80%) diff --git a/finance-tracker/.gitignore b/finance-tracker/.gitignore new file mode 100644 index 0000000..40b878d --- /dev/null +++ b/finance-tracker/.gitignore @@ -0,0 +1 @@ +node_modules/ \ No newline at end of file diff --git a/.prettierrc b/finance-tracker/.prettierrc similarity index 100% rename from .prettierrc rename to finance-tracker/.prettierrc diff --git a/finance-tracker/app.js b/finance-tracker/app.js index 23715a7..2d817ae 100644 --- a/finance-tracker/app.js +++ b/finance-tracker/app.js @@ -1,13 +1,8 @@ import { addTransaction, - getTotalIncome, - getTotalExpenses, - getBalance, - getTransactionsByCategory, getLargestExpense, printAllTransactions, printSummary, - getTransactionsByType, getTotalTransactions, } from './finance.js'; @@ -15,15 +10,13 @@ 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', - }, -]; +const transaction = { + type: 'expense', + category: 'Personal Care', + amount: 20, + description: 'baber shop', + date: '2025-01-30', +}; addTransaction(transaction); @@ -33,7 +26,7 @@ printSummary(); const largestExpense = getLargestExpense(); console.log( chalk.bold( - `\nLargest Expense: ${largestExpense[0].category} (€${largestExpense[0].amount})`, + `\nLargest Expense: ${largestExpense.category} (€${largestExpense.amount})`, ), ); diff --git a/finance-tracker/finance.js b/finance-tracker/finance.js index a83a01e..eb2ae67 100644 --- a/finance-tracker/finance.js +++ b/finance-tracker/finance.js @@ -2,10 +2,10 @@ import { transactions } from './data.js'; import chalk from 'chalk'; export function addTransaction(transaction) { - if (!transaction[0].id) { - transaction[0].id = transactions.length + 1; + if (!transaction.id) { + transaction.id = transactions.length + 1; } - transactions.push(...transaction); + transactions.push(transaction); return transactions; } @@ -44,17 +44,15 @@ export function getTransactionsByCategory(category) { } export function getLargestExpense() { - const filteredTransactions = getTransactionsByType('expense'); - let largestTransaction = []; - let largestExpense = null; - for (const filteredTransaction of filteredTransactions) { - if (filteredTransaction.amount > largestExpense) { - largestExpense = filteredTransaction.amount; - largestTransaction.pop(filteredTransaction); - largestTransaction.push(filteredTransaction); + let largest = null; + for (const transaction of transactions) { + if (transaction.type === 'expense') { + if (largest === null || transaction.amount > largest.amount) { + largest = transaction; + } } } - return largestTransaction; + return largest; } export function printAllTransactions() { diff --git a/package-lock.json b/finance-tracker/package-lock.json similarity index 100% rename from package-lock.json rename to finance-tracker/package-lock.json diff --git a/package.json b/finance-tracker/package.json similarity index 80% rename from package.json rename to finance-tracker/package.json index a2e2a7a..c552168 100644 --- a/package.json +++ b/finance-tracker/package.json @@ -5,7 +5,7 @@ "main": "app.js", "type": "module", "scripts": { - "start": "node ./finance-tracker/app.js" + "start": "node ./app.js" }, "dependencies": { "chalk": "^5.6.2"