Let's get started with the math object.
I have already created a new folder and have named it as math object and I have two files, my HTML
and my JavaScript file and I have linked up my JavaScript to my HTML down below.
Okay, good.
So let's get our hands dirty with the math objects.
Now we have empty JavaScript file.
All right, so let's get started with the math object.
Where the math objects functions are easy to understand.
So I'm going to explain it in one single video and the challenges comes in when we put them into use
cases.
So you are going to have a lot of coding challenges after I exploit you the meaning and when to use
this map functions.
So the first one that we will talk about, so let me comment here and say math functions.
Or object.
So the first one that you would talk about is what you call moth dot absolute.
So like I said, the PDF guy, we provided an order description to be inside the PDF.
So the first one we would talk about is moth dot absolute.
Then it takes in the number that I want to have the absolute.
Oh.
So what is the moth that absolute.
Well it retains the absolute value.
And that is only positive numbers will be retained if you pass a negative number to see positive.
So let's have a look.
So let's assign a variable called, let's say answer is equal to none.
I'll in math, which is a global method built into the browser.
So you see that I have it as that this object is not me or you who made it, so I'm going to use it.
It's a built in method.
So if I make use of dot, you see, I have all the functions or methods that we can use to operate on
Mathematica operations.
So what I want is a base meaning the absolute.
Then I pass in the number I want to have observed with.
So let's say I want to pass in ten.
You get it?
So I'll come down here and console that log and sum.
So if I check the console, have a look.
Indeed.
We have ten.
What about if I put negative here?
You see that I still have ten.
If I add, let's say sorry if I say 10.1, see that I still have 10.1.
If I add any number to it, I still have it.
So the is retains what the absolute value of the number and that is it about the math dot absolute.
So again, let me comment this one.
The next one is what you call moth dot round.
The name is even descriptive, which is moth dot round.
So it is used to retain a value of x rounded to the nearest integer.
So here we go.
So the same way counts as say, my answer is equal to math dot round.
If I pass in, let's say 5.5, if I save it, we see that I have C's.
It has rounded to the nearest whole number or integer.
So if I pass in, let's say 5.1, you see that it ignored one because it is not up to five.
If I pass, let's say four.
I see that I still have four.
I still have five, but if it's more than five or equal to five is going to run it to the nearest integer.
So this would be six.
That is the round for you.
The next method of function is called C0.
Great.
So let me copy this one.
Let me copy this one here and now.
Paste it here and change this one.
Let me comment this one now.
This is called SEAL.
We still have the math object.
So what does the seal mean?
Well, it retains the value of X rounded to the nearest integer.
So let's have a look at differences here.
All of these ones returns to the nearest integer.
That is a round.
But let's have a look at the round.
If I save this one, you see that?
Well, I got six.
Okay, so what about if it is less than five?
Let's see one.
But after it has, what, round to six?
You see that?
So it doesn't take whether the decimal places is less than or equal to five.
As soon as you see decimal place, less than zero is going to run into the nearest whole number.
That is one trick about that.
Okay.
The next one is going to be what is called flow and this one is commonly used.
So you also commonly this you.
Sorry, I see you.
Okay.
So let me copy this one.
Come down here, paste it.
And now this is called the floor.
We are going to floor you.
Okay?
Comment this one.
And here we change this one to floor.
Okay.
So if I save this one before I save it, let me explain to you what is the floor?
The all retains the value of x rounded to the nearest integer.
But the cool thing is that there are some special use cases here.
If I save this one, you can see that I got five again.
Ideally it makes sense because one is less than five.
So if I add even two here or three here is still five.
To me, this one makes sense.
But what about if I add five?
Well, it's two.
Five.
What about I put nine?
Well, it's two five.
So this one, it doesn't take the decimal place into consideration whether it's less than or equal to
five or more than five.
It doesn't care.
When we come to money issue, I don't use floor, but it depends on some use cases whereby you don't
care about decimal places.
So this one will change the whole number, whether it has a decimal place or not.
That is flow for you.
All right.
The next function is called math dot square root s q r t.
So let me comment this one out here.
Okay.
So this one has made me remember it way back high school.
Do a mathematical class, a question like, Can you find me a square root of two or 25?
Well, so it is a time.
So after you've gone through this, you can create your own application to perform mathematical calculations
like this, operations like this ones.
So here all I need is I'll pass in a dot zero alt square root.
So what would be the square root of five?
I guess it's simple mathematics, right?
Before I save it, it's going to be what, square root of 25?
Well, I think this one that makes sense to me.
Well, what about 25?
What, about 36 going to be six, right?
Yes, six because six times six is 36 and you're going to be 25 two.
Five times five is what 25 is going to be five.
That is correct for you.
So we can create a simple application just to find a square root of any number, whatever.
If I ask you to find the square root of, say, 1.8888444333.
Can you tell me at a spot?
Nope.
But for this one, if I save it, I get the answer, which is
1.37435236868133649.
Is it?
So for this one, we can run it up to the nearest whole number by using flow to get only one if you
want.
In that way, why not?
We can go back that way.
That is square root for you.
Okay, so let me bring it back to 25, which makes sense in a normal sense to me here.
So what about the next method is what we call OC.
Find the power of accession number.
These are all, I mean, high school mathematical operations.
So let me copy this one and then come down here.
Let me comment this one out.
Now, here we go.
This is called the power.
Find the power of two numbers.
Okay.
The cool thing about the power here is that this one takes into argument.
Let's say you want to find two.
Power three.
What does it mean?
22% means that.
Let me show you.
Maybe if some of you have forgotten.
So let's say that I want to find.
I mean the power of two.
Power three.
Let's say two.
Power three.
Like this.
One, two.
Part three.
Like this one.
What does it mean?
This means that multiply two three times, you're going to be two by two by two.
If it is ten, multiply two by itself ten times.
That's the square root.
The power.
Sorry.
So here we're going to pass in two arguments.
Let's say two, comma, three.
This means that to power three, it's going to be eight, right?
So two times two times two is eight.
So let's save it.
And indeed I have eight.
That is that.
The next one is what we call the minimum.
For example, I like this method a lot.
For example, if we have some arrays of numbers, I ask your place, can you find me the minimum number
in this particular array?
Do you remember in our previous coding challenges, we have some scenarios like this whereby I ask you
that find me the minimum number of two, a minimum number between numbers or a of numbers.
So it is a time.
So here, let me copy this one.
And now come down here and commit this one out and this one just type in min.
And if we see the IntelliSense, we have min value and two values, one one here and then retain the
smaller of the set of supplied numeric expressions.
The name is in descriptive here, so return the minimum value provided to this function.
If I save this one, these two which are which of these is a minimum or the list if I save it?
Indeed it going to be two.
Yes, indeed.
Whatever.
If I pass in, let's say nine and negative two is negative two is a minimum number among these two.
So we can work on array than just providing these two.
I mean, numbers, that is a mean for you.
What about the maximum it returns, the maximum number provided.
So if I change this one to maximum the same process, I get nine because nine is larger than negative
two.
The next method is what you call the random.
And this method is really crucial because we can make a lot of use cases out of it.
This is maximum.
Yeah, and this is random.
So for this method we can create awesome guessing game applications and which are times two.
This method is being used to generate, let's say, token or code when trying to log into application.
That maybe five digit code has been sent to your email.
Please check it out.
It's been done using the moth dot random.
So let's see how you're going to do this one.
So moth dot random.
I mean, generate random numbers between two numbers.
Okay.
So let's say that I want to generate random numbers for this one.
All I need is that I'll just bring in random as that.
If I do this one, I save it.
Now, if I console.log the answer, let's see.
We're going to be any time I refresh the page, you see that I get different numbers in the console.
That's the beauty of it.
So for this one, by default, by default, it's from 0 to 1.
And these are inclusive from where 0 to 1, zero is exclusive and one is between zero and one meaning
you cannot get up to zero and you can never get to one.
Okay, so let's see this by default.
What about if I want to provide my own random numbers between, let's say, two values, let's say get
a random numbers between ten, five and ten or 20 and 30.
How are you going to do that?
This task at first glance is going to be a little bit worth to you, but just pay attention and you
can receive the formula.
Anytime you want to get something of that sort, just make reference to it and that is it.
So let's say that gets random random numbers between.
Two numbers.
Yes.
So let me have enough room here because it is very important.
So let me comment this one here for now.
Great.
So now here we go.
Let's have let's look at the syntax, how we can get I mean, random numbers between two numbers.
Good.
So the alternative is that you bring in math like that.
Let me have a comment that will not have something like this one.
So you bring in the math dot random like this one.
Okay, math that random.
And then you're going to multiply that one as a function like this one.
Then you multiply by the maximum minus minimum plus one and then plus the minimum.
So so this one, it goes like this one I'm going to subtract.
I'll bring in mine parentheses.
Like this one, and then I would take my maximum minus minimum, then I would add plus one and then
a plus one here, and then I would add plus my minimum.
So this is a formula to get random numbers between two numbers.
So again, the maximum number minus the minimum number plus one and then plus the minimum number.
Let's have a look.
For example, let's say example here, example, let's say that gets.
Random numbers.
Between.
Ten and 20.
So let's see how we're going to do this one.
So I would say const rant random is equal to.
Remember our being in first of all my math floor which is math.
Sorry, random math, but random as my function call.
The next one is how multiply my multiplication sign like that.
And next is this syntax.
My parentheses max minus minimum plus one.
So here we go.
Is going to be maximum the maximum, which is, let's say 20.
Right?
We go here and then minus the minimum and then plus one.
Then I will add my minimum, which is ten.
So we can create a function to make it more dynamic and we get back to that very soon.
All right.
So let's cancel out the random number here as round.
Okay, so I got 13.59 and the decimal places.
So let's say like you don't want to help the decimal places, you can use the floor method to floor
and get a whole number.
So here I'll cut the result and pass into that floor.
Remember the floor?
What it does do not pass in my random number.
Then I get a whole number upon every random numbers.
That is it about the the minimum.
Sorry, the random function or method.
So with this one, I will simplify it in nice way Lacey.
I'll just pass in the so I'll copy this one.
So this one the same thing as by passing everything inside math dot floor.
Then I'll paste my logic inside and then I can remove the floor from here like that.
And then I have it so the same as before.
This syntax is what you are going to see most in some code.
All right.
Now that we have the overview of some of the methods in this video, let's get started with our coding
challenge.