All right, let's see how I'm going to solve this particular challenge.
Okay.
So first of all, it says that create a function to find the average of two numbers.
So the first one gives me the name of the function that I'm supposed to use.
So let's go.
So come down here.
My solution to this challenge is this.
Well, the name of my function, let me say function.
Just not fiction, but its function average.
And then I would take in number one and then number two as my parameters.
And in here I would declare const or let's let me select here because I want override the final result.
So I would say my AVG average is equal to.
Nam one plus num two divided by two.
So I need to put them in.
Parentheses like this one because of both.
Right.
But mine's meaning this one.
Let me show you the board.
My technique here.
This is how we can multiply or add operations in a sequential manner.
So you see that B means bracket.
First, you need to handle the bracket first and then of means multiplication.
If there is one, then you go ahead and division.
Okay.
That's why I put them inside like this one divided by two.
Okay, so let's continue.
So if I get that one, if I.
Let me console.log as quickly from this function to avoid just returning the assignment variable like
that.
So let me console.log the results, the average.
Okay.
So if I save this function, let me return to be more precise because you want to write good code.
Okay, let's return the average.
Okay.
So here, if I assigned the results equal to the average, then I pass in my two numbers.
Let's say I want to find average between let's say two and four.
What would be the answer to platform is six over two is going to be three.
So if I console that log the the result like that, let's see in the console.
Yes, indeed.
We have three over here.
Okay.
What about if I provide, let's say something like seven, you see that I get 4.5.
What about if I let's see, two point something like this one.
Let's see the random numbers I get.
We see that I get this one.
And the question is, is that we need to run up to a whole number.
So here we can use map that floor to floor this.
So here I'll use map dot floor and then I'll pass in my average result.
So this one, I will never get a decimal place, okay?
Because this one have run up to some digit or better still, we can also convert this one to whole maybe
two decimal places or one decimal places using to fix.
Okay, so it's up to you.
It's up to you.
So here is I.
Let's check.
What about if I provide, let's say some the first one I want to add, let's say four and then the percent
provide a string, let's say also as let's say two.
Okay, let's see, we're going to be based on what's going through what would be the answer, right?
We are trying to contact this a string.
Right, a string.
So we are trying to four plus two here.
We give me something like this.
According to what we've learned so far, going to be four and two because this course string concatenation.
But if we divide by two, then it'll be undefined or null because how can we divide a number by a string?
By a number.
So let's see the result for this.
So as a programmer, we need to avoid all these issues because we don't trust the user.
So if I save it, you see that it's not a number.
So how can we check for that?
So here, let's say check, check if there are numbers.
And I'm referring to they means my arguments.
They are numbers.
But not bombast, but instead numbers.
So here we check and here you can use the number built in method.
So let's say if again the we pass the the number and then you pass in the number.
Number one.
If indeed it's a number if and you're going to check for two conditions, if two of them are numbers,
then you can go ahead and then display whatever you want or we can go ahead and do our logic.
So here and the same number, then you pass in number two.
Okay.
If that's the case, then if it is true, then we can go ahead and then retain this.
But we can reverse this one, making it as worth, not a number.
So here I can copy this one and put it inside.
Here else I can return a text and say that please provide.
And number.
So let's see, Nancy.
I get what?
Please provide a number, because what I did is not a number.
What about if I make this one as ten and indeed one is string?
Yes, indeed.
We get plays, provide a number.
So it's working.
The next video.
Let's have another coding challenge.