From 119d5eb35f6ac3f1351da13072ee3f208edeb49b Mon Sep 17 00:00:00 2001 From: hannahwn Date: Tue, 3 Feb 2026 21:07:36 +0100 Subject: [PATCH 01/15] initialized and installed chalk --- package-lock.json | 86 +++++++++++++++++++++++++++++++++++++++++++++++ package.json | 24 +++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 package-lock.json create mode 100644 package.json 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..6978cfd --- /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/HackYourAssignment/c55-core-week-4.git" + }, + "keywords": [], + "author": "", + "license": "ISC", + "type": "commonjs", + "bugs": { + "url": "https://github.com/HackYourAssignment/c55-core-week-4/issues" + }, + "homepage": "https://github.com/HackYourAssignment/c55-core-week-4#readme", + "dependencies": { + "chalk": "^4.1.2" + } +} From f46eaf2be10e97cf4c2411143b68d1f4583228a1 Mon Sep 17 00:00:00 2001 From: hannahwn Date: Tue, 3 Feb 2026 21:40:11 +0100 Subject: [PATCH 02/15] setup prettier --- .gitignore | 157 ---------------------------------------------- package-lock.json | 19 ++++++ package.json | 3 + 3 files changed, 22 insertions(+), 157 deletions(-) diff --git a/.gitignore b/.gitignore index 2b76d7c..c2658d7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,158 +1 @@ -# System files -.DS_Store -Thumbs.db -[Dd]esktop.ini - -# hyf -.hyf/score.json - -# Editor and IDE settings -.vscode/ -.idea/ -*.iml -*.code-workspace -*.sublime-project -*.sublime-workspace -.history/ -.ionide/ - -# Logs -logs -*.log -npm-debug.log* -yarn-debug.log* -yarn-error.log* -lerna-debug.log* - -# Diagnostic reports (https://nodejs.org/api/report.html) -report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json - -# Runtime data -pids -*.pid -*.seed -*.pid.lock - -# Directory for instrumented libs generated by jscoverage/JSCover -lib-cov - -# Coverage directory used by tools like istanbul -coverage -*.lcov - -# nyc test coverage -.nyc_output - -# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) -.grunt - -# Bower dependency directory (https://bower.io/) -bower_components - -# node-waf configuration -.lock-wscript - -# Compiled binary addons (https://nodejs.org/api/addons.html) -build/Release - -# Dependency directories node_modules/ -jspm_packages/ - -# Snowpack dependency directory (https://snowpack.dev/) -web_modules/ - -# TypeScript cache -*.tsbuildinfo - -# Optional npm cache directory -.npm - -# Optional eslint cache -.eslintcache - -# Optional stylelint cache -.stylelintcache - -# Optional REPL history -.node_repl_history - -# Output of 'npm pack' -*.tgz - -# Yarn Integrity file -.yarn-integrity - -# dotenv environment variable files -.env -.env.* -!.env.example - -# parcel-bundler cache (https://parceljs.org/) -.cache -.parcel-cache - -# Next.js build output -.next -out - -# Nuxt.js build / generate output -.nuxt -dist - -# Gatsby files -.cache/ -# Comment in the public line in if your project uses Gatsby and not Next.js -# https://nextjs.org/blog/next-9-1#public-directory-support -# public - -# vuepress build output -.vuepress/dist - -# vuepress v2.x temp and cache directory -.temp -.cache - -# Sveltekit cache directory -.svelte-kit/ - -# vitepress build output -**/.vitepress/dist - -# vitepress cache directory -**/.vitepress/cache - -# Docusaurus cache and generated files -.docusaurus - -# Serverless directories -.serverless/ - -# FuseBox cache -.fusebox/ - -# DynamoDB Local files -.dynamodb/ - -# Firebase cache directory -.firebase/ - -# TernJS port file -.tern-port - -# Stores VSCode versions used for testing VSCode extensions -.vscode-test - -# yarn v3 -.pnp.* -.yarn/* -!.yarn/patches -!.yarn/plugins -!.yarn/releases -!.yarn/sdks -!.yarn/versions - -# Vite logs files -vite.config.js.timestamp-* -vite.config.ts.timestamp-* - 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 6978cfd..239e93d 100644 --- a/package.json +++ b/package.json @@ -20,5 +20,8 @@ "homepage": "https://github.com/HackYourAssignment/c55-core-week-4#readme", "dependencies": { "chalk": "^4.1.2" + }, + "devDependencies": { + "prettier": "^3.8.1" } } From 5754282556bb05f940468e8ba773d20d8026f584 Mon Sep 17 00:00:00 2001 From: hannahwn Date: Tue, 3 Feb 2026 21:56:28 +0100 Subject: [PATCH 03/15] created a prettier file --- .prettierrc | 6 ++++++ package.json | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 .prettierrc diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..94e30b6 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,6 @@ +{ + "singleQuote": true, + "semi": true, + "trailingComma": "es5", + "printWidth": 80 +} diff --git a/package.json b/package.json index 239e93d..6dde4c1 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "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" + "test": "echo \"Error: no test specified\" && exit 1" ,"format": "prettier --write \"**/*.js\"" }, "repository": { "type": "git", From e786176eff0422daa03a02f28173cd8930cf7229 Mon Sep 17 00:00:00 2001 From: hannahwn Date: Tue, 3 Feb 2026 22:21:22 +0100 Subject: [PATCH 04/15] added transaction objects --- finance-tracker/data.js | 53 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/finance-tracker/data.js b/finance-tracker/data.js index d7863ff..d817cf8 100644 --- a/finance-tracker/data.js +++ b/finance-tracker/data.js @@ -1,2 +1,53 @@ // 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-04' + }, + { + id: 2, + type: 'expense', + category: 'rent', + amount: 1000, + description: 'Pay for the house', + date: '2025-01-06' + }, + { + id: 3, + type: 'income', + category: 'Making hair', + amount: 5000, + description: 'Money earned working in the weekend', + date: '2025-01-29' + }, + { + id: 4, + type: 'expense', + category: 'travel card', + amount: 1500, + description: 'Pay NS for travel card', + date: '2025-01-26' + }, + { + id: 5, + type: 'expense', + category: 'tours', + amount: 2000, + description: 'Saving money for vacancy', + date: '2025-01-10' + }, + { + id: 6, + type: 'expense', + category: 'Subscriptions', + amount: 1000, + description: 'Busuu,Netflix,Spotify,Data', + date: '2025-01-15' + }, + + +]; From 08f3b2be2998d9728323d82e0ceceb1b68a87b37 Mon Sep 17 00:00:00 2001 From: hannahwn Date: Wed, 4 Feb 2026 11:04:50 +0100 Subject: [PATCH 05/15] adds transactions and gets total income --- finance-tracker/app.js | 3 +++ finance-tracker/data.js | 1 + finance-tracker/finance.js | 20 ++++++++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/finance-tracker/app.js b/finance-tracker/app.js index 7cfcd07..821f6fe 100644 --- a/finance-tracker/app.js +++ b/finance-tracker/app.js @@ -1,3 +1,6 @@ // This is the entrypoint for your application. // node app.js + + + diff --git a/finance-tracker/data.js b/finance-tracker/data.js index d817cf8..7061877 100644 --- a/finance-tracker/data.js +++ b/finance-tracker/data.js @@ -51,3 +51,4 @@ const transactions = [ ]; +module.exports = { transactions }; diff --git a/finance-tracker/finance.js b/finance-tracker/finance.js index ac2118f..62300c5 100644 --- a/finance-tracker/finance.js +++ b/finance-tracker/finance.js @@ -1,9 +1,29 @@ +const { transactions } = require("./data.js"); + function addTransaction(transaction) { // TODO: Implement this function + let maxId = 0; + for (let i = 0; i < transactions.length; i++) { + if (transactions[i].id > maxId) { + maxId = transactions[i].id; + } + } + transaction.id = maxId + 1; + + transactions.push(transaction); + console.log("→ Added transaction #" + transaction.id); + } function getTotalIncome() { // TODO: Implement this function + let total = 0; + for (let i = 0; i < transactions.length; i++) { + if (transactions[i].type === "income") { + total += transactions[i].amount; + } + } + return total; } function getTotalExpenses() { From 9dab125275182347cc41971125ed2e9ca04d4834 Mon Sep 17 00:00:00 2001 From: hannahwn Date: Wed, 4 Feb 2026 11:06:34 +0100 Subject: [PATCH 06/15] gets total expenses --- finance-tracker/finance.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/finance-tracker/finance.js b/finance-tracker/finance.js index 62300c5..a5a5a0e 100644 --- a/finance-tracker/finance.js +++ b/finance-tracker/finance.js @@ -28,6 +28,13 @@ function getTotalIncome() { function getTotalExpenses() { // TODO: Implement this function + let total = 0; + for (let i = 0; i < transactions.length; i++) { + if (transactions[i].type === "expense") { + total += transactions[i].amount; + } + } + return total; } function getBalance() { From c53e8bb7da7a38e07d280bf40c9590b75519971c Mon Sep 17 00:00:00 2001 From: hannahwn Date: Wed, 4 Feb 2026 11:07:35 +0100 Subject: [PATCH 07/15] gets remaining money --- finance-tracker/finance.js | 1 + 1 file changed, 1 insertion(+) diff --git a/finance-tracker/finance.js b/finance-tracker/finance.js index a5a5a0e..c34815e 100644 --- a/finance-tracker/finance.js +++ b/finance-tracker/finance.js @@ -39,6 +39,7 @@ function getTotalExpenses() { function getBalance() { // TODO: Implement this function + return getTotalIncome() - getTotalExpenses(); } function getTransactionsByCategory(category) { From cf0621d76b8b94e2bd5b5359d6241a235170e37e Mon Sep 17 00:00:00 2001 From: hannahwn Date: Wed, 4 Feb 2026 11:09:07 +0100 Subject: [PATCH 08/15] gets by category --- finance-tracker/finance.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/finance-tracker/finance.js b/finance-tracker/finance.js index c34815e..06e96b7 100644 --- a/finance-tracker/finance.js +++ b/finance-tracker/finance.js @@ -44,10 +44,18 @@ function getBalance() { function getTransactionsByCategory(category) { // TODO: Implement this function + let found = []; + for (let i = 0; i < transactions.length; i++) { + if (transactions[i].category === category) { + found.push(transactions[i]); + } + } + return found; } function getLargestExpense() { // TODO: Implement this function + } function printAllTransactions() { From 873b584e2835176e6a1a85db88a25a4c06f5f78e Mon Sep 17 00:00:00 2001 From: hannahwn Date: Wed, 4 Feb 2026 11:10:09 +0100 Subject: [PATCH 09/15] gets largest expense --- finance-tracker/finance.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/finance-tracker/finance.js b/finance-tracker/finance.js index 06e96b7..3a025a4 100644 --- a/finance-tracker/finance.js +++ b/finance-tracker/finance.js @@ -55,9 +55,20 @@ function getTransactionsByCategory(category) { function getLargestExpense() { // TODO: Implement this function - + let largest = null; + let maxAmount = -1; + + for (let i = 0; i < transactions.length; i++) { + let t = transactions[i]; + if (t.type === "expense" && t.amount > maxAmount) { + largest = t; + maxAmount = t.amount; + } + } + return largest; } function printAllTransactions() { // TODO: Implement this function + } \ No newline at end of file From 3dfdb127d5e7a4b63b84370b9c828cd4b445244e Mon Sep 17 00:00:00 2001 From: hannahwn Date: Wed, 4 Feb 2026 11:12:30 +0100 Subject: [PATCH 10/15] prints all transactions --- finance-tracker/finance.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/finance-tracker/finance.js b/finance-tracker/finance.js index 3a025a4..a071c28 100644 --- a/finance-tracker/finance.js +++ b/finance-tracker/finance.js @@ -70,5 +70,20 @@ function getLargestExpense() { function printAllTransactions() { // TODO: Implement this function + console.log("\n=== Transactions ==="); + console.log("ID | Type | Amount | Category | Description"); + console.log("-----------------------------------------------"); + for (let i = 0; i < transactions.length; i++) { + let t = transactions[i]; + let sign = t.type === "income" ? "+" : "-"; + console.log( + t.id + " | " + + t.type.padEnd(8) + " | " + + sign + t.amount.toFixed(2).padStart(6) + " | " + + t.category.padEnd(10) + " | " + + t.description + ); + } + console.log("-----------------------------------------------\n"); } \ No newline at end of file From 581eabffdaa2a0479415273df942b2d51a2fe803 Mon Sep 17 00:00:00 2001 From: hannahwn Date: Wed, 4 Feb 2026 11:14:46 +0100 Subject: [PATCH 11/15] added imports and exports --- finance-tracker/app.js | 10 +++++++++- finance-tracker/finance.js | 13 ++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/finance-tracker/app.js b/finance-tracker/app.js index 821f6fe..ea34713 100644 --- a/finance-tracker/app.js +++ b/finance-tracker/app.js @@ -1,6 +1,14 @@ // This is the entrypoint for your application. // node app.js - +const { + addTransaction, + getTotalIncome, + getTotalExpenses, + getBalance, + getTransactionsByCategory, + getLargestExpense, + printAllTransactions +} = require("./finance.js"); diff --git a/finance-tracker/finance.js b/finance-tracker/finance.js index a071c28..84696f1 100644 --- a/finance-tracker/finance.js +++ b/finance-tracker/finance.js @@ -86,4 +86,15 @@ function printAllTransactions() { ); } console.log("-----------------------------------------------\n"); -} \ No newline at end of file +} + + +module.exports = { + addTransaction, + getTotalIncome, + getTotalExpenses, + getBalance, + getTransactionsByCategory, + getLargestExpense, + printAllTransactions +}; \ No newline at end of file From 7b349c88cd069acc80ed95d10d5ea5f615b918fc Mon Sep 17 00:00:00 2001 From: hannahwn Date: Wed, 4 Feb 2026 14:11:33 +0100 Subject: [PATCH 12/15] adds transaction and prints all --- finance-tracker/app.js | 23 +++++++++++++++++++++-- finance-tracker/finance.js | 2 +- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/finance-tracker/app.js b/finance-tracker/app.js index ea34713..8031079 100644 --- a/finance-tracker/app.js +++ b/finance-tracker/app.js @@ -7,8 +7,27 @@ const { getBalance, getTransactionsByCategory, getLargestExpense, - printAllTransactions -} = require("./finance.js"); + printAllTransactions, +} = require('./finance.js'); +console.log("Starting application..."); +printAllTransactions(); +console.log("Total income: €" + getTotalIncome().toFixed(2)); +console.log("Total expenses: €" + getTotalExpenses().toFixed(2)); +console.log("Current balance: €" + getBalance().toFixed(2)); + +addTransaction({ + type: "expense", + category: "Food", + amount: 9.50, + description: "Lunch sandwich" +}); + +addTransaction({ + type: "income", + category: "Sale", + amount: 40, + description: "Sold old headphones" +}); \ No newline at end of file diff --git a/finance-tracker/finance.js b/finance-tracker/finance.js index 84696f1..100a7ba 100644 --- a/finance-tracker/finance.js +++ b/finance-tracker/finance.js @@ -1,4 +1,4 @@ -const { transactions } = require("./data.js"); +const chalk = require("chalk"); function addTransaction(transaction) { // TODO: Implement this function From f658fbe5610bc94eea55665a16eafc7b10fbf764 Mon Sep 17 00:00:00 2001 From: hannahwn Date: Wed, 4 Feb 2026 14:38:37 +0100 Subject: [PATCH 13/15] prints all after adding transactions --- finance-tracker/app.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/finance-tracker/app.js b/finance-tracker/app.js index 8031079..e9411f0 100644 --- a/finance-tracker/app.js +++ b/finance-tracker/app.js @@ -30,4 +30,11 @@ addTransaction({ category: "Sale", amount: 40, description: "Sold old headphones" -}); \ No newline at end of file +}); + +console.log("\nAfter adding transactions:"); +printAllTransactions(); + +console.log("New total income: €" + getTotalIncome().toFixed(2)); +console.log("New total expenses: €" + getTotalExpenses().toFixed(2)); +console.log("New balance: €" + getBalance().toFixed(2)); \ No newline at end of file From 3e1b0c9f16d20c75e9cf42d49d7ab0488ece6e9b Mon Sep 17 00:00:00 2001 From: hannahwn Date: Wed, 4 Feb 2026 14:41:15 +0100 Subject: [PATCH 14/15] finds largest expense --- finance-tracker/app.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/finance-tracker/app.js b/finance-tracker/app.js index e9411f0..05509c4 100644 --- a/finance-tracker/app.js +++ b/finance-tracker/app.js @@ -37,4 +37,15 @@ printAllTransactions(); console.log("New total income: €" + getTotalIncome().toFixed(2)); console.log("New total expenses: €" + getTotalExpenses().toFixed(2)); -console.log("New balance: €" + getBalance().toFixed(2)); \ No newline at end of file +console.log("New balance: €" + getBalance().toFixed(2)); + +console.log("All 'Food' transactions:"); +console.log(getTransactionsByCategory("Food")); + +console.log("\nLargest expense:"); +const biggest = getLargestExpense(); +if (biggest) { + console.log("#" + biggest.id + " " + biggest.category + " €" + biggest.amount + " – " + biggest.description); +} else { + console.log("No expenses found."); +} \ No newline at end of file From b18e9c3488779b2856dc8e99486e19846dc11f26 Mon Sep 17 00:00:00 2001 From: hannahwn Date: Wed, 4 Feb 2026 15:05:00 +0100 Subject: [PATCH 15/15] added chalk properties --- finance-tracker/app.js | 50 +++++++++++++++++++++---------------- finance-tracker/finance.js | 51 +++++++++++++++++++++----------------- 2 files changed, 57 insertions(+), 44 deletions(-) diff --git a/finance-tracker/app.js b/finance-tracker/app.js index 05509c4..eaeb637 100644 --- a/finance-tracker/app.js +++ b/finance-tracker/app.js @@ -10,42 +10,50 @@ const { printAllTransactions, } = require('./finance.js'); - -console.log("Starting application..."); +console.log('Starting application...'); printAllTransactions(); -console.log("Total income: €" + getTotalIncome().toFixed(2)); -console.log("Total expenses: €" + getTotalExpenses().toFixed(2)); -console.log("Current balance: €" + getBalance().toFixed(2)); +console.log('Total income: €' + getTotalIncome().toFixed(2)); +console.log('Total expenses: €' + getTotalExpenses().toFixed(2)); +console.log('Current balance: €' + getBalance().toFixed(2)); addTransaction({ - type: "expense", - category: "Food", - amount: 9.50, - description: "Lunch sandwich" + type: 'expense', + category: 'Food', + amount: 9.5, + description: 'Lunch sandwich', }); addTransaction({ - type: "income", - category: "Sale", + type: 'income', + category: 'Sale', amount: 40, - description: "Sold old headphones" + description: 'Sold old headphones', }); -console.log("\nAfter adding transactions:"); +console.log('\nAfter adding transactions:'); printAllTransactions(); -console.log("New total income: €" + getTotalIncome().toFixed(2)); -console.log("New total expenses: €" + getTotalExpenses().toFixed(2)); -console.log("New balance: €" + getBalance().toFixed(2)); +console.log('New total income: €' + getTotalIncome().toFixed(2)); +console.log('New total expenses: €' + getTotalExpenses().toFixed(2)); +console.log('New balance: €' + getBalance().toFixed(2)); console.log("All 'Food' transactions:"); -console.log(getTransactionsByCategory("Food")); +console.log(getTransactionsByCategory('Food')); -console.log("\nLargest expense:"); +console.log('\nLargest expense:'); const biggest = getLargestExpense(); if (biggest) { - console.log("#" + biggest.id + " " + biggest.category + " €" + biggest.amount + " – " + biggest.description); + console.log( + '#' + + biggest.id + + ' ' + + biggest.category + + ' €' + + biggest.amount + + ' – ' + + biggest.description + ); } else { - console.log("No expenses found."); -} \ No newline at end of file + console.log('No expenses found.'); +} diff --git a/finance-tracker/finance.js b/finance-tracker/finance.js index 100a7ba..73c47b1 100644 --- a/finance-tracker/finance.js +++ b/finance-tracker/finance.js @@ -1,4 +1,6 @@ const chalk = require("chalk"); +const { transactions } = require("./data.js"); + function addTransaction(transaction) { // TODO: Implement this function @@ -11,15 +13,14 @@ function addTransaction(transaction) { transaction.id = maxId + 1; transactions.push(transaction); - console.log("→ Added transaction #" + transaction.id); - + console.log(chalk.green('→ Added transaction #' + transaction.id)); } function getTotalIncome() { // TODO: Implement this function - let total = 0; + let total = 0; for (let i = 0; i < transactions.length; i++) { - if (transactions[i].type === "income") { + if (transactions[i].type === 'income') { total += transactions[i].amount; } } @@ -30,7 +31,7 @@ function getTotalExpenses() { // TODO: Implement this function let total = 0; for (let i = 0; i < transactions.length; i++) { - if (transactions[i].type === "expense") { + if (transactions[i].type === 'expense') { total += transactions[i].amount; } } @@ -39,12 +40,12 @@ function getTotalExpenses() { function getBalance() { // TODO: Implement this function - return getTotalIncome() - getTotalExpenses(); + return getTotalIncome() - getTotalExpenses(); } function getTransactionsByCategory(category) { // TODO: Implement this function - let found = []; + let found = []; for (let i = 0; i < transactions.length; i++) { if (transactions[i].category === category) { found.push(transactions[i]); @@ -55,12 +56,12 @@ function getTransactionsByCategory(category) { function getLargestExpense() { // TODO: Implement this function - let largest = null; + let largest = null; let maxAmount = -1; for (let i = 0; i < transactions.length; i++) { let t = transactions[i]; - if (t.type === "expense" && t.amount > maxAmount) { + if (t.type === 'expense' && t.amount > maxAmount) { largest = t; maxAmount = t.amount; } @@ -70,25 +71,29 @@ function getLargestExpense() { function printAllTransactions() { // TODO: Implement this function - console.log("\n=== Transactions ==="); - console.log("ID | Type | Amount | Category | Description"); - console.log("-----------------------------------------------"); - + console.log(chalk.bold('\n=== Transactions ===')); + console.log(chalk.bold('ID | Type | Amount | Category | Description')); + console.log('-----------------------------------------------'); + for (let i = 0; i < transactions.length; i++) { let t = transactions[i]; - let sign = t.type === "income" ? "+" : "-"; + let sign = t.type === 'income' ? '+' : '-'; console.log( - t.id + " | " + - t.type.padEnd(8) + " | " + - sign + t.amount.toFixed(2).padStart(6) + " | " + - t.category.padEnd(10) + " | " + - t.description + t.id + + ' | ' + + t.type.padEnd(8) + + ' | ' + + sign + + t.amount.toFixed(2).padStart(6) + + ' | ' + + t.category.padEnd(10) + + ' | ' + + t.description ); } - console.log("-----------------------------------------------\n"); + console.log('-----------------------------------------------\n'); } - module.exports = { addTransaction, getTotalIncome, @@ -96,5 +101,5 @@ module.exports = { getBalance, getTransactionsByCategory, getLargestExpense, - printAllTransactions -}; \ No newline at end of file + printAllTransactions, +};