Let me show you how I'm going to solve this challenge.
Here you go.
So first one is to write a function that takes away.
So let's solve the first one.
So here is my function.
Let me use function expression system around any bigger so that I can see it.
Okay.
So const is going to average.
Numbers or average now is equal to my function and that so this function we take in an array and then
my parenthesis inside.
Let's see.
The function should take an array and return the the average.
So here we go.
So how can we get an average?
For example, if we have these records, let's say we have a ray of let's say four, comma, five,
comma, six, and then come on to, like I said, we're going to add these together, four plus five
plus six plus two over two and over two over 1 to 3 for the number of items present in the array.
So in the record as that's if I do this one, I'm able to get my average.
So how can I or how can you write some code to do something like this one?
Remember, for this addition here we can use reduce.
And to be able to get the total length we can use length method.
So we need reduce method and then the length method to get it done, as simple as that.
So let's see.
So let's start with the first the second one.
Okay.
Yeah, yeah.
That's the function should be taken away and retain the average.
So here I would say calculate the average.
Here we go.
So cost.
I would say the average is equal to the array.
Our pass in dot reduce.
And then inside I'll pass in my callback function.
I have access to my accumulator and my current value as that, remember, because you talk about it
before and now my parentheses, and that's why my initial value is that.
So let me expand this.
So here I'm going to return return the accumulator.
Plus.
The agreement applies the current value, not EU, but when you here are here wherever.
So as current value as that.
So I got it right.
So next is I need to return from this function.
So to return the average I got here.
So let's say return the result.
Results from the function.
So here you go.
Return the average.
And remember, you convert this one.
Okay, I'll show you what I mean here.
So next is let's call it and see how we can solve the next one.
We are done.
We are done with these two, as is this one and this one for the meantime, let's try our code first.
So it comes, let's say answer is equal to our call.
My function is average avg number.
Then I'll pass in any array I think I have.
The amount.
Yeah, let me let me comment out this one because I have the verbal name being declared down here.
Yes.
So I'll pass in an array.
So let's say I'll pass in in line here.
Let's say I have two, three, four, five.
So let's console.log.
Console.log.
The answer.
Now we've got for let's see if we are right.
Two plus three is five.
Plus four is nine plus five is 14.
Now, we got it.
So but the question says we should find the average.
So here in divide this one, divide this one by OC for the meantime, before we do that, what will
the average let's do it manually.
We have to this is 14 right divided by let's say we have two, one, two, three, four.
So 14 here let me have we're going to have 14 over four.
What would be the answer for this one?
Well, let's see.
So next is and divide the number of record here, the number of items in the record.
And I can get it as what this array length which is coming from this.
So this array is this one.
So I can append over, let's say divided by the array dot length.
And that is if I save it.
Let's check the answer.
Now we got 3.5.
You see, the last thing is you should convert it to two decimal places because.
Okay, let's ask maybe more.
Let's see, we have nine.
We have two.
Let's see.
We see that we got some random numbers, but I want to run up to two decimal places.
So we need to use what is called fix.
So on the result here we can use to fix to fix.
Remember the arithmetic we are combining all together.
I'll pass in as two.
So let's say with this one now see that I got 4.12 to 2 decimal places, but it says that we should
return in a format that we don't assign.
So here.
I can remove this one, and now I can inject some temperature here.
Now I say that.
Well, I should add a dollar sign.
Right?
So here I will add my dollar sign like that.
And next is I want to add dynamic value.
Dollar sign sees it now paste the results, then remove the semicolon here.
Let's see now I have $4.17.
That is it, guys.
The next video.
Let's see.
Another coding challenge.