Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions finance-tracker/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"semi": true,
"singleQuote": false
}
3 changes: 3 additions & 0 deletions finance-tracker/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// This is the entrypoint for your application.
// node app.js
import * as finance from "./finance.js";

console.log(finance.printAllTransactions());
console.log(finance.printAllTransactions());
43 changes: 42 additions & 1 deletion finance-tracker/data.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,43 @@
// Place here the transaction data array. Use it in your application as needed.
const transactions = [];
export const transactions = [
{
id: 1,
type: "income",
category: "salary",
amount: 3000,
description: "Monthly salary",
date: "2025-01-15",
},
{
id: 2,
type: "expense",
category: "transport",
amount: 60,
description: "Monthly train pass",
date: "2025-01-15",
},
{
id: 3,
type: "expense",
category: "groceries",
amount: 85.5,
description: "Weekly grocery shopping",
date: "2025-01-17",
},
{
id: 4,
type: "expense",
category: "rent",
amount: 1200,
description: "January apartment rent",
date: "2025-01-01",
},
{
id: 5,
type: "income",
category: "freelance",
amount: 450,
description: "Website design project",
date: "2025-01-22",
},
];
97 changes: 82 additions & 15 deletions finance-tracker/finance.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,94 @@
function addTransaction(transaction) {
// TODO: Implement this function
import { transactions } from "./data.js";
import chalk from "chalk";

export function addTransaction(transaction) {
transactions.push({ ...transaction });
}

export function getTotalIncome() {
let totalIncome = 0;
for (const transaction of transactions) {
if (transaction.type === "income") {
totalIncome += transaction.amount;
}
}
return chalk.green(totalIncome);
}

function getTotalIncome() {
// TODO: Implement this function
export function getTotalExpenses() {
let totalExpenses = 0;
for (const transaction of transactions) {
if (transaction.type === "expense") {
totalExpenses += transaction.amount;
}
}
return chalk.red(totalExpenses);
}

function getTotalExpenses() {
// TODO: Implement this function
export function getBalance() {
let balance = 0;
let index = 0;

while (index < transactions.length) {
const { type, amount } = transactions[index];
// Income increases the balance, anything else (expense) decreases it
if (type === "income") {
balance += amount;
} else {
balance -= amount;
}
index++;
}
return balance >= 0 ? chalk.cyan(balance) : chalk.red(balance);
}

function getBalance() {
// TODO: Implement this function
export function getTransactionsByCategory(category) {
const matchingTransactions = [];
for (const transaction of transactions) {
if (transaction.category === category) {
matchingTransactions.push(transaction);
}
}
return matchingTransactions;
}

function getTransactionsByCategory(category) {
// TODO: Implement this function
export function getLargestExpense() {
let largestExpenseAmount = 0;
let largestExpense;

for (let transaction of transactions) {
if (
transaction.type === "expense" &&
largestExpenseAmount < transaction.amount
) {
largestExpenseAmount = transaction.amount;
largestExpense = transaction.category;
}
}
return `${chalk.yellow(largestExpense)} (${chalk.red(largestExpenseAmount)})`;
}

function getLargestExpense() {
// TODO: Implement this function
export function printAllTransactions() {
let finalMessage = "💰 PERSONAL FINANCE TRACKER 💰\n\nAll Transactions:\n";
for (let transaction of transactions) {
finalMessage += `${transactions.indexOf(transaction) + 1}.`;
if (transaction.type === "income") {
finalMessage += `[INCOME] `;
} else {
finalMessage += `[EXPENSE] `;
}
finalMessage += `${chalk.yellow(transaction.category)} - €${transaction.type === "income" ? chalk.green(transaction.amount) : chalk.red(transaction.amount)} (${transaction.description})
`;
}
return finalMessage;
}

function printAllTransactions() {
// TODO: Implement this function
}
export function printSummary() {
return `📊 FINANCIAL SUMMARY 📊
Total Income: €${getTotalIncome()}
Total Expenses: €${getTotalExpenses()}
Current Balance: €${getBalance()}

Largest Expense: ${getLargestExpense()}
Total Transactions: ${transactions.length}`;
}
86 changes: 86 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "c56-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/SaadZetouny/c56-core-week-4.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "module",
"bugs": {
"url": "https://github.com/SaadZetouny/c56-core-week-4/issues"
},
"homepage": "https://github.com/SaadZetouny/c56-core-week-4#readme",
"dependencies": {
"chalk": "^4.1.2"
}
}