Skip to content

Angelika S - #10

Open
angelika-arch wants to merge 1 commit into
HackYourAssignment:mainfrom
angelika-arch:main
Open

angelika-arch wants to merge 1 commit into
HackYourAssignment:mainfrom
angelika-arch:main

Conversation

@angelika-arch

Copy link
Copy Markdown

No description provided.

@angelika-arch angelika-arch changed the title Complete Week 4 Personal Finance Tracker Angelika S Nov 25, 2026
@amgando

amgando commented Nov 26, 2026

Copy link
Copy Markdown

hi @angelika-arch,

solid work again, it runs first time, the output matches the example and the formatting is clean throughout

a few ideas below to push it further. if you have any questions, ping me on slack or find me on monday so we can talk in person

@amgando amgando left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

solid work, well done

everything the assignment asks for is here and it's clean. the comments are about the next level: saying the same thing with less code, and making sure every function has actually run

next week i'd like to see one of the bonus challenges from you. removing a transaction by id is a good one to start with

} No newline at end of file
console.log(`All Transactions: `);
for (let i = 0; i < transactions.length; i++) {
const { type, description, amount, category } = transactions[i];

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nice use of destructuring, and the indentation is spot on in this PR

Comment on lines 8 to 26
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 expense = 0;
for (let i = 0; i < transactions.length; i++) {
if (transactions[i].type === 'expense') {
expense += transactions[i].amount;
}
}
return expense;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

these two functions are the same 8 lines with one word changed. this is DRY again, from week 3: pull the shared part into one function and pass in what's different

function getTotalByType(type) {
  let total = 0;
  for (const transaction of transactions) {
    if (transaction.type === type) {
      total += transaction.amount;
    }
  }
  return total;
}

function getTotalIncome() {
  return getTotalByType('income');
}

function getTotalExpenses() {
  return getTotalByType('expense');
}

notice i also used for...of (see here). when you don't need the index i for anything, for...of gives you each item directly and there's no transactions[i] to repeat. keep the classic for for when you do need the position, like the numbering in printAllTransactions

alternative, shorter. you'll see this style a lot in other people's code, so it's worth being able to read it

function getTotalByType(type) {
  return transactions
    .filter((transaction) => transaction.type === type)
    .reduce((total, transaction) => total + transaction.amount, 0);
}

filter keeps the items that match, reduce folds them into one value. the assignment asked for a loop, so yours is the right answer here

let kind = '';
if (type === 'income') {
kind = '[INCOME]';
} else {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

else here means "anything that isn't income". so a transaction with type: 'incme' (a typo) or type: 'transfer' is printed as an expense, in red, and nothing tells you

this is a very common one. handle the cases you know, and decide on purpose what happens to everything else

if (type === 'income') {
  kind = '[INCOME]';
} else if (type === 'expense') {
  kind = '[EXPENSE]';
} else {
  kind = '[UNKNOWN]';
}

Comment on lines +62 to +71
let kind = '';
if (type === 'income') {
kind = '[INCOME]';
} else {
kind = '[EXPENSE]';
}
const coloredKind = type === 'income' ? chalk.green(kind) : chalk.red(kind);
const coloredCategory = chalk.yellow(category);
const coloredAmount =
type === 'income' ? chalk.green(`€${amount}`) : chalk.red(`€${amount}`);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

this block asks "is this income?" three times (lines 63, 68 and 71). you can ask once and reuse the answer. to do that you need one new idea, so i'll show the idea first

the idea: a function can be stored in a variable

const paint = chalk.green;   // no () here, so nothing runs yet.  paint now holds the function
paint('hello');              // exactly the same as chalk.green('hello')

chalk.green('hello') with () runs the function and gives you coloured text. chalk.green without () is the function itself, and you can keep it for later

using it here

step 1, ask once and pick the colour function

const isIncome = type === 'income';
const paint = isIncome ? chalk.green : chalk.red;

step 2, build each piece and give it a name

const label = paint(isIncome ? '[INCOME]' : '[EXPENSE]');
const amountText = paint(`€${amount}`);
const categoryText = chalk.yellow(category);

step 3, the line that prints is now easy to read

console.log(`${i + 1}. ${label} ${description} - ${amountText} (${categoryText})`);

functions are values in javascript. you can store them and pass them around like numbers or strings. more on this here: first-class functions

Comment on lines 4 to 6
function addTransaction(transaction) {
// TODO: Implement this function
transactions.push({ ...transaction });
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

here's a question: how do you know this function works?

it doesn't look like anything calls addTransaction or getTransactionsByCategory. maybe you tested them and removed those lines afterwards, that's fine. as the code stands they never run. a few lines in app.js that use them and print the result would show it

addTransaction({
  id: 6,
  type: 'expense',
  category: 'food',
  amount: 80,
  description: 'Groceries',
  date: '2025-01-20',
});

console.log(getTransactionsByCategory('salary'));

you'll need to export them from finance.js first

i'd like you to go one step further here and let the program check itself. node has a built-in assert module

import assert from 'node:assert';

assert.strictEqual(getTotalIncome(), 7000);
assert.strictEqual(getTransactionsByCategory('salary').length, 2);

if a value is wrong the program stops and tells you which line. this is the start of testing. node also ships a test runner for when you want to take it further

console.log(chalk.bold(`Total Expenses: €${chalk.red(getTotalExpenses())}`));
console.log(
chalk.bold(
`Current Balance: €${getBalance() >= 0 ? chalk.cyan(getBalance()) : chalk.red(getBalance())}`,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

getBalance() is called three times in this one line, and each call loops over every transaction twice. call it once and give the result a name

const balance = getBalance();
const paint = balance >= 0 ? chalk.cyan : chalk.red;

console.log(chalk.bold(`Current Balance: €${paint(balance)}`));

with 5 transactions the speed makes no difference. the line is shorter and easier to read this way

Comment thread package.json
},
"homepage": "https://github.com/angelika-arch/c56-core-week-4#readme",
"dependencies": {
"chalk": "^4.1.2"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

small one. the assignment says chalk@4.1.2 and you have ^4.1.2. the ^ means "4.1.2 or any newer 4.x", it's what npm adds by default (more here)

your package-lock.json pins the exact version anyway, so in practice you're getting 4.1.2. if you ever want the exact version written in package.json too

npm install chalk@4.1.2 --save-exact

while you're in this file, add a start script so anyone can run the app without knowing where the entry file is

"scripts": {
  "start": "node finance-tracker/app.js"
}

then npm start just works. that's the first thing most developers try in a project they don't know

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants