Skip to content
Open
157 changes: 0 additions & 157 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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-*

6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"singleQuote": true,
"semi": true,
"trailingComma": "es5",
"printWidth": 80
}
56 changes: 56 additions & 0 deletions finance-tracker/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,59 @@
// This is the entrypoint for your application.
// node app.js
const {
addTransaction,
getTotalIncome,
getTotalExpenses,
getBalance,
getTransactionsByCategory,
getLargestExpense,
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.5,
description: 'Lunch sandwich',
});

addTransaction({
type: 'income',
category: 'Sale',
amount: 40,
description: 'Sold old headphones',
});

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("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.');
}
54 changes: 53 additions & 1 deletion finance-tracker/data.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,54 @@
// 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-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'
},


];
module.exports = { transactions };
80 changes: 79 additions & 1 deletion finance-tracker/finance.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,105 @@
const chalk = require("chalk");
const { transactions } = require("./data.js");


function addTransaction(transaction) {
// TODO: Implement this function
let maxId = 0;
for (let i = 0; i < transactions.length; i++) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice... you think of adding an id property with a consecutive value to the transaction object. Bonus points!

if (transactions[i].id > maxId) {
maxId = transactions[i].id;
}
}
transaction.id = maxId + 1;

transactions.push(transaction);
console.log(chalk.green('→ 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() {
// 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() {
// TODO: Implement this function
return getTotalIncome() - getTotalExpenses();
}

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
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
}
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' ? '+' : '-';
console.log(
t.id +
' | ' +
t.type.padEnd(8) +
' | ' +
sign +
t.amount.toFixed(2).padStart(6) +
' | ' +
t.category.padEnd(10) +
' | ' +
t.description
);
}
console.log('-----------------------------------------------\n');
}

module.exports = {
addTransaction,
getTotalIncome,
getTotalExpenses,
getBalance,
getTransactionsByCategory,
getLargestExpense,
printAllTransactions,
};
Loading