Welcome, everyone.
In today's lecture, what we are trying to do is we're going to calculate the calories that we are inserting
into our application from user and right.
So so far, we haven't done this and it's going to be completely new.
We're going to use some functions, but we're not going to probably manage to display them here back
in the front app.
But we will try to implement those functions here today.
So in our mean app controller, we do have this function function for adding the calories.
So I'm going to create additional function for that will hold.
So I'm not going to put it here.
Right.
I'm not going to put it in this body here, but I can do additional functions for calculating the calories
in this here.
This function.
Just let me calculate.
So now maybe you're a little bit confused and you say, OK, now why don't you write this function when
it's about calculating the calories, just weight in our calories control.
So we're not going to do this.
We are going to do it here still.
We're going to do that.
I mean, but here are we going to do a little bit different?
So here we add calories and that is also connected with the calories.
But this is coming from the front end, right?
So here we are going to calculate the calories, but we're going to call we're going to create a couple
of separate functions here that will go into our calories controller.
The one on top.
Right.
So I'm not going to scroll down.
So I'm going to say calculate calories here.
You should put as much as more comments as you need.
So the first things is to compute compute calories or to create some function so that some of the functions
that I would like to use, I'm going to say calculate.
And I'm going to say here, I'm going to change it, I'm going to say calories, calculation's
function in here, I will have some functions inside and I will say, OK, I would like to say calculate.
Come on, what's happening?
Calculate calories in here, I will have I will call the calories controller and I will call these compute
calories.
This is a function that it will not take any parameters.
But this function we haven't created yet, so we're dysfunctional will go this function.
You already know it will go in our calories controller.
Right.
So it will go go up, up, up here, right.
In our calories controller.
In our in our public.
Right.
So I'm just going to write it down here.
And we're going to call the same name, right?
So we're going to say compute calories
in our call is a function, right?
Oops.
Now, remember to put a comma here, otherwise it will not work.
So put it just in front of the result.
OK, here I'm just going to say print so we're not you're not going to use this after that, we're going
to comment.
But is good print the entire storage object, right.
Or return the entire storage object in here.
We will calculate.
The cameras, no ear, we will do a couple of calculations, but in order to do that, we need to,
for example, to create additional function and that function will be private function.
Right.
So this function, lets say total calories, I'm going to call it total calories in and it will accept
one parameter here.
These parameters will be a time.
But because we are not passing anything to the function, I just going to manually choose to do.
And if I'm going to call this total calories from here, I'm just going to create for the brain.
Right.
So these function we have not created.
So where this function will go.
OK, so remember this.
We are in return.
We are returning.
We are in the public.
So I just want this total calories to go above.
So calculate the total calories in and that will go here.
So I'm just going to create more in here.
This function will be our core, so more total calories and it will be equal to a function with the
time.
Right now I can put that type here.
Just make sure you put a semicolon tab here, because we manually, manually add this and we're going
to read comments here.
Don't worry about it now.
We manually add the type there.
Now I've got I will get the time.
So what I want to do, remember that we have in storage all of the calories in this is an area for add
in burns when we are adding more and more here, this area is expanding where it contains more and more
information.
And so we need to look through this one.
And what are we going to do?
We're going to use for each.
So, number one, we're going to see a loop
loop through the area based on its type.
Right.
So I'm going to create more total here and that will be zero.
I'm going to make it to be a zero now for each.
I'm just going to explain how the foreach loop is working.
You already know.
So the foreach accepts callback right.
Function and callback with W.L..
So how is the name of the foreach.
So first we need to use storage and all calories and we need to based on the type it will be for the
at Audibert.
So we already select storage, add calories and for example we select this one.
So if we are adding based on that or it's going to be Burne, so we select that area.
But how we can look through this so we can say for each just like that and it's asep a callback function.
Now inside this, a set couple of parameters are exactly three parameters.
I'm going to say callback function with that's why stop it with three parameters and that will be value
current value current index and the entire
area.
So, so yeah I have that mista here.
Now this function accepts three, as I told you, three parameters.
Right.
And for now we don't need the entire we don't need the index, we don't need the entire area because
we are looking through this area.
But what we need here, I just need the current right.
The current current value.
OK, now so how are we going to do this?
Because I have this total here.
I'm just going to write the function, the core function, and after that I'm going to explain it how
plus equal or this is same as total equal total plus.
And after that we continue doing what we are doing right.
So that this, this and this is same.
Right.
We use that one before.
Now you know how I'm not going to I'm going to use the short version so total that will be equal to
the current calories.
Right, dot, dot, dot, sorry, multiplied by the current quantity.
Now how I get this one well inside this one, what we have, we have the calories and we have quantity.
That's why I can grab those.
I have these four values here.
I can use them.
I can get the idea, but I don't need the 80.
So the total will hold the current calories multiplied by the quantity.
So if we have 50 calories and two of the quantity, that means it's a hundred and it will be saved here
in total.
So mind we just initialize the total before you.
So how would this work?
OK, let me just write down a simple example.
For example, in our we are working with our Ed Henry here, and I'm going to create a nursery here
saying, OK, I have 50.
I say five fifty five.
What else?
I have one hundred and five and thirty six, for example.
You can choose whatever colors you want in here will be the index, which we are not using at the moment.
Right.
Start from the zero position.
It goes to the first, it goes to the second and third.
So we have three actually elements inside, but we have four because we're starting from this zero.
So how is this going to work?
OK, let's put it like that and we're going to say, number one, the total will be equal to zero.
Right.
Or I'm not going to just put it as a number one.
I'm just going to say the total will be equal to zero because that is what we have before we going in
looping through the Herry.
So after that, the total will be total, plus the value, the current value of this index.
So it's going to be five.
Right.
So that will be zero plus five.
It will be five.
How cool is this right now, the next one.
So this is going to be number one.
Number two is going to be total equal total plus this element here with the value of fifty five and
that will be five plus fifty five and that will be sixteen.
So see how we are doing before was zero plus five, it's five.
And we are adding this value as a total here.
I don't want to write five plus fifty fifty five to go exactly like it's math, but I just wanted to
make it a little bit simpler.
So total it will be equal to total plus one hundred and five in the value will be 60 plus a hundred
and five and that will be one sixty five in the last step that this is going to loop through the last
element.
So it's going to iterate how many times.
Four times.
Right.
And we will see total plus thirty six and that will be equal.
One hundred sixty five plus thirty six and that will give us two a one.
So the value that I'm getting here is going there.
The value that I'm getting here is going there, the value that I'm getting here is going there is the
final result will be two oh one.
Right.
So this is I'm going to leave it like this is what I can do is actually I can just create.
Just like that, and, yeah, it's going to simplify the way.
Now this is the way how are we going to calculate how this for each loop?
So we are just using the storage, we are getting the all calories and this object contains to erase.
And based on the time we are looping through that particular area, if it's for burning, real looping
there in here, we call those we just call this function with this name, which just providing the time.
Right.
And now what we need to do is actually inside our global storage here.
We haven't done this inside our global storage object.
We will need to have all some calories.
Herry, we need to have additional Aeris in order or we need to have another object with two values
there.
Right.
In order to save this data, because I'm going to get some result here like two one.
So where I'm going to save it.
So I'm going to save it back as an object here in it.
We need comma here and I'm going to see and it will be so I'm going to say it will be zero and burn.
It will be zero.
So when I started this program, when I started when I initiated this program, this all will have we
will they will have add and burn to zero.
But now when I do this, I do have to 201 for the end.
But at the moment I'm just looping through them.
So now I need to store them here.
So how I'm going to store it, that's very easy.
So same storage dot and now I'm going to see all in inside, all based of our type.
That's why we use typewrite.
That's why we use add and burn.
Based on our time.
It will be add or burn.
I'm going to store this total variable here so the value of the total will go there.
So if it's for burning, it will save it there.
So it's not going to be zero anymore.
Right, because we are looking through all the items inside.
We are looking through all of their values and we're putting back in the storage.
So that is pretty much it.
We are done now when I call this, when it will be called, they will loop through these areas here
and they will see the results here.
So I hope I'm clear now, like make another comment here, make another comment and see inside our global
storage storage object.
Add another object where we store the total that the total value of the end and burn calories.
I hope this is going to say so, that's why we create this er all object here.
OK, so we need to work now here, right.
So we do have that one.
Don't worry, we're going to use that.
So we are we're not nearly there, but we now we need to calculate how much calories have been left.
So how are we going to do this.
Oh yeah.
Let's say I'm going to say storage.
So storage is for this reason, I need to create an additional variable there.
We have to show property and I will say total calories per per day, OK?
And the total calories per day will be storage.
Not all that at minus storage that all that burn.
So imagine that this will return.
This will create this will save in the in the storage to five.
Right.
And this will save one hundred sixty five.
Right.
And now I'm accessing the storage, I'm accessing this property which I don't have it at the moment,
which will we will create it now.
So we need to create OK, I'm going to create a couple of them.
So just do this together with me.
After that it will be all clear, total calories per day and I'm going to be set to zero.
Total calories per day.
I'm going to put an additional property here, Minkow per day, and I'm going to set the two thousand
three hundred.
Remember when I told you that you can set your own value?
So if you want these in your body mass index, based on the size, based on the height, there is a
function.
You can find it online and you can say, OK, I need to burn this.
I need to take this amount of calories per day.
I just I'm putting this average because for girls around two thousand and four boys, it's a two thousand
five hundred.
So that's why I just put it somewhere in between.
Right.
And yeah.
Now I was also percentage.
And that will be zero again and what else we do have here and remaining, and that will be zero.
Wow.
OK, so that will go for this remaining.
That will go for this percentage and for the total calories and burn calories will go here.
OK, let's start working.
So I have these in our storage, these four properties.
So what I'm going to do with them.
So first I call the storage of total calories, which will be zero is basically the same as what we
did here with the we just put it here right now, you know, and I say, OK, now the total calories
per day, it will be two hundred and five minus one hundred sixty five.
And that will give me a result of forty.
So the total calories per day, I will have 40.
Right.
And I will store them here now.
Mean calories per day.
OK, what are we going to do with mean calories per day.
So we're going to use that mean calories.
Don't worry, we're going to use that for the calculating the remaining right.
Okay.
Where we were.
Yeah, now I'm going to say store.
Stora, I'm going to put a store story back to our global storage, what total calories per day?
Come on.
Oh.
Now here, I'm going to copy this one and I'm going to put it like this story back in our global storage
now mean calories per day, right?
Remaining.
Sorry.
So how are we going to do this storage not remaining here, so I grab that one, it's 2300 and I'm going
to say, OK, storage not remaining, I'm going to call it again.
And it will be minus the storage, that total calories per day.
Remember that we see this guy here, he's going to have 40, for example, right, you save it 40 and
it will be 2300, minus 40.
Right.
That is the logic.
So the last part is we need to calculate the percentage.
So how are we going to do this?
I'm just going to copy this guy here and calculate.
In the store, that percentage.
And here should be saying calculate in store right now here we are going to call the percentage, the
percentage is zero at the moment, right.
So how are we going to do that?
So storage, not total calories, just going to remove this because we don't need it and that I'm going
to divide it by the storage that mean calories per day that the formula used to multiply by 100.
Right.
So what we're going to have, for example, I have let's say that I have the example is percentage.
It will be, for example, seven hundred divided by two thousand three hundred.
And after that, we're going to multiply by a hundred.
OK, so let's use the calculator and see seven hundred divided by two thousand three hundred.
That will give me this.
If I multiply by 100, it will give me 30 percent.
But look, it is it will give me that will give me thirty point forty three percent so we can use another
math object and use Routon method to calculate the percentage.
Remember we have done this, so I'm going to call this math round and that will give me a nice round
of this one.
So but I need I need to grab this cut it here.
I'm going to put up inside the brackets.
Otherwise it will work.
But it's better like this.
So we're done.
This is how are we going to calculate this is how we are going to do now.
What's next?
Well, next year I need to return everything that I have just calculated.
So I'm just going to create a function here called return.
Return calories and I'm going to say function and comma here and I'm going to put a comment here saying,
OK, so what will we get data from the storage storage and
then return it as an object?
Right.
We have done this in past.
So how what what is the goal here?
The goal here is to return all of this.
Right.
So I'm just going to copy them.
And what I'm going to do, I'm just going to return them as a one big object.
Right, because I'm going to return multiple data as an object.
So what I'm going to do, I'm going to say, OK, I'm just going to put it like that and I'm going to
say, OK, today, calories, you can write any name you want.
So here I'm going to call the storage that today's calories.
Right, because we're not doing anything else.
Now, here what I will say, I will say total edit and how I'm going to grab the total edit.
The total will be storage.
Not all that bad.
Right.
So I'm just going to call a comma here total burn.
So we need to do storage that old burn.
Right.
I'm grabbing all of these that we're going to need.
Right.
So I'm grabbing the what we stored from this function here, from this little here.
What else we need, comma should be there remaining.
I'm going to cut it from here remaining.
I'm going to say, OK, storage
remaining and the last one will be percentage.
And I don't need this mingalar.
If you want, you can return that one as well.
Right.
The percentage it will be storage, that percentage it pretty much will return.
Whatever.
What what was the entire what we what we calculate here or compute here and here.
So that is pretty much done.
So what we need to do now, now we need to do a couple of things.
We need to call this one somewhere.
We need to call this function in two places.
You're going to see why.
OK, we are going down here.
So already called the computer calories, but now I just want to get the calories right and I'm going
to create a variable here, calories, and I'm going to store what we return very so sorry calories.
And that will be equal to what calories controller and that will be able to get access to return calories,
this function here.
And it didn't require any parameters.
So I'm just going to get get computed.
From compute calories at function in what was the other function name, let's see.
Total, I think total calories.
So we are going to get all all these data from these two, right.
And like that, no, I did have this one inside here, the only thing that remains is to control all
of these calories and we are done.
I know this lecture will be super, super long, but I'm sorry about this, guys.
Yeah, now, one more thing here, we need to call this function, calculate calories.
Remember, we haven't call it anywhere and I just want to call it just right below here.
So while we are doing this, otherwise it will not be initiated, right?
It will not be called in.
The calculation will not be done.
So calculate
related
functions.
And I'm going to call it here, calculate calories like that so I can do it because it's in in this
scope here.
So I hope this is done.
Now, I'm just going to close this.
I hope it's going to work.
So I'm going to add a calorie.
I'm going to inspect it.
And if we have some error, it will be fun because we will need to what we will need to get rid of that
error.
OK, now for the food.
I'm going to say salad one hundred seventy seventy five point this.
So we're going to add it.
Here it is.
So I have this to these calories.
It will be this total.
Add it.
How cool is this to burn the zero remaining in?
Why is giving me this error here?
I'm not sure the remaining should be go back.
So we need to find out what's wrong with the remaining right instead of here putting the missing calories,
but yeah, the mean calories per day, I just put that remaining and that's why it's not working.
OK, here one.
But let's hope is going to be fix it.
Salyut one hundred seventy eight point nine.
OK, here it is now.
It's working now from 2300.
This 178 is substract and the percentage is eight.
OK, let's add another one.
Ice cream, one fifty five calories.
Cool.
Now it's added seven one hundred and seventy eight before plus fifty five is going to give you this.
Let's add one more.
What will that coffee there.
And I will say one.
I will say how cool is this.
Now let's Bernsten running one lap.
Two hundred and fifty and this percentage should drop and this should go up.
Right.
That's the logic.
Right.
The percentage should drop is drop and the remaining is this almost.
Yeah.
And today's calories are burned today at it.
It will be this to the burn this and these to these calories is the when you subtract this minus this.
So I hope you enjoy this lecture and we are getting there now the last part we need to put these results
that we have it here right.
Updated in our front in the front app.
Right.
So that's pretty much it.
Super long lecture.
Sorry about this.
Hope to see you in the next one.