In this video, let's work on the tool to balance the total expenses and then the total income.
Well, before I continue, I want to give the expenses a color of red.
So let's get back to the components, and that is our C component dashboard and then the account details.
So let's allocate the expenses to to balance here expenses.
And for this one, I want to give it a color of red and give this one as 400 or let's see, 600.
Yeah.
So let's save it and let's see.
And indeed, we have it.
What about the income?
I want to give it a color of green.
So the income is this one 9000, which is this one?
I want to give it a color of green for success.
Yes, yes, yes, yes, yes.
So I think this one's okay.
So how can we calculate?
Guys, remember, we have two types of transactions.
You see, we have income and then the expenses.
So we are going to look through to find all transaction by type income and then you are going to use
reduce to add up and also and the same way for expenses.
And we can do this one in two ways.
One is we are going to filter out all transaction with a TIBCO income and then after that we are going
to use reduce contacts.
So let's have a comment here.
So the first one is going to be as calculates the total income calculate.
To a tight income.
And here we go.
Cost total income is equal to the account.
Remember, we have all the transactions and then we are going to use reduce to reduce that.
So here you go and I'll pass in a callback function as that and I have the first variable as a accumulator
and then the actual transaction.
So in case you are not familiar with the reduced method and the essays of this course, I cover them
in details.
But don't worry.
I've explained to you as I move on to inside here, I'm going to pass in the callback function.
I'll start the next one.
Going to be the initialize.
So by default I'll provide zero.
And inside here, if you console.log the account as accumulator, you're going to see that by default
you're going to be zero.
So upon every loop you're going to add the transaction amount.
But before we continue, unless we make some checking here and here we go on a transaction, if the
transaction type, if you console.log the transaction, you're going to see what is called transaction
type if it is equal to income.
Then we are going to add up, retain the account plus the transaction dot amount else retained the account
that is in share one.
Great.
So here if you consider that log the console, that log the total income.
Let's see.
So let's check the console and let's see what we have.
Line number 21, we have zero, which is this where is so weird, right?
I think our condition here is not working well.
So let's try to console, log this to make sure if you're going to have true or false, let's save it.
And now let's see.
You can see that we have false meaning that our condition is not correct.
So let's console.log the transaction type and let's see.
I see we have undefined meaning I'm making some typing mistake about the transfer.
Oh sorry.
It is spelling of transaction as is here.
So I believe you guys.
Sorry.
So we're going to be transaction with s.
We see how one word and one letter can confuse us.
So let's save it and let's see.
And I have $5,000, so let's see.
Well, so let's check it out and see.
Yeah, we have one with income 5000.
So if I get back to back end and I want to add a transaction of income and sum around, let me change
this one to let's see 1000 and call this one as I got this one for my salary to support my project.
So it's sent.
And let me refresh it.
And let me check the cargo some.
This one.
Let me check the console.
And I got 6000 because 5000 plus 1000 is equal to 6000.
So now I can replace with this one by remember.
In case a user add that initial balance to the account upon creating, we need to add that one.
Let's console.log the account and let's add the transaction and let's add the initial balance.
So let's look at the account on line number four.
So let's get back to the coin.
So again, line number 12, which is this one?
Oh yeah, this one, the last one.
And let's look at the initial balance for this account, yet you can see that this account have initial
balance of 1000.
So what you're going to do is that we are going to add plus the initial balance.
So inside the value here where we are displaying it.
So inside where we have the total balance.
So now inside the total income, which is this one, total expenses and income, we are going to add
the value.
So here are going to be.
Dollar sign carrier base is going to be the total income and then plus the account Dot's initial balance.
So we are going to have what is called 7000, right?
So let's see, let's check it out.
And indeed we have 7000.
So why from the duplicate dollars here.
Yeah, so let's see.
And indeed this is my total income.
So what would be the total expenses?
So we're going to be copy and paste.
So here you go and we are going to copy this one.
And down here, let's pass it here and going to be total expenses
and here is going to be as total expenses and here type is expenses and that is it.
So let's inject inside the expenses placeholder, which is this, and here we go.
Sorry.
Here.
Total expenses.
Yeah.
Then you see.
Yeah, we have 10,000 because we have two expenses.
One 5000, one 5000 equal to 10,000.
So what about the total balance?
I'm going to pretty easy, right?
Just subtracting.
So here we are going to subtract the total income plus the accounts, the initial balance, initial
balance then minus the total expenses.
And here you go.
And indeed we are of.