Next up, a one off video on something called the math object built into JavaScript out of the box for
free.
We get access to this thing called math with a capital M, and it's just a collection of properties
and methods that have to do with math in some way.
So we have methods like rounding or round.
We have a method called abs for absolute value.
We have constants, mathematical constants like PI, the value of PI 3.1415, nine, and so on.
Which at one point I knew the first 200 digits of and like fourth, fifth grade, we had a competition
at my school.
Anyway, I've lost almost all of that.
So this math object is here in the console.
I don't have to do anything.
I can just reference it and I can actually just hit enter on math and expand this.
So we haven't talked about objects yet and you don't need to really understand what an object is in
JavaScript except for the fact that it's a collection of properties and methods.
So one option would have been to make just a variable called PI that we always have access to, and
another one called E, if you know Euler's number or you know the square root of two, that could just
be available anywhere, right?
So I could just do that or JavaScript could do that.
It doesn't because instead of just polluting the namespace, which basically means instead of just having
a bunch of variable names taken up Java scripts approach was to combine all of those into a single object.
So they're all nested under this thing called math.
So I have to access math dot pi or math dot E for Euler's number.
You probably won't really need either of these, at least not with what we're going to focus on for
now.
But they are there.
But what I want to focus on are the methods.
These methods range from things that you probably won't need to use at all, like, I don't know, ARC
ten or what is it cosine sine square root you may want to know about, but the ones I'll be showing
are mainly around random numbers and a little bit around rounding and flooring your numbers so you can
read more about the math objects.
On MDM, there's a page for every single method.
If you wanted to learn about sine and how it works, not necessarily the math behind it, but how the
sine method works and if it expects any arguments and how they work and all of that.
But I'll just show a couple of these methods right off the bat.
First of all, we've got one called Floor Math Floor.
We'll take a number with a decimal and chop off that decimal so it's not rounding.
This could be 9999999, which would round up to 24.
It's just chopping it off.
So that's floor.
We also have ceiling or seal, so I could have 34.1 and it's going to round it up no matter what.
So 34.9999 is going to seal or ceiling to 3 to 35 and 34.01.
We'll also round up or ceiling up to 35 so those maybe not that useful on their own, but what I'm going
to show you now is how we generate random numbers.
So there is a built in method to that math object.
And this is really the whole reason I'm showing you the math object in the first place.
There's a built in method called random, and random is going to give us a random decimal number between
zero and one, but it won't actually include one.
So it's going to be between zero and, you know, 9.999999999 math random, just like that.
So every time I call that it is a method, I need those parentheses.
Every time I call it, I get a different very long decimal usually.
Well, sometimes that's fine, but most of the time if we're trying to get a random number, we might
be trying to get a random number between one and two.
We might want a coin flip or zero and one where it's just integers, the whole number, one, whole
number too.
Like what's your favorite number between one and 100?
I don't want to hear, you know, 94.12815934.
I just want to hear 91 or 92 or whatever the number is.
So to do that requires a bit more work.
We don't actually have a method called random integer or random int or something like that many other
programming languages do in JavaScript we don't, but we can do some math.
We can take a number like this and then floor it in order to get a whole number.
So it's a multi step process.
Let's say we want random numbers between one and ten, I can call math at random.
So that gives me a very long decimal between zero and one, then I can multiply by ten.
So now I'm looking at 5.9, six one, blah blah blah.
Then I can floor that to just get five.
Okay.
So let's try that.
Let's do a number between one and five.
So math.
Dot random and I'm going to multiply by five.
I want a number between one and five, so that gives me 3.7 this time.
Next time it's 2.0 no.
That gave me 1.8, 4.1, 4.2, 0.8.
And then I'm going to math for that, but I want to floor the entire thing.
So what I did here was put it in a variable and then floor that, but that's a bit extra.
I don't really need this.
I don't need this step on its own.
So I'm going to do a math floor.
Remember what floor does it chop's off the decimal so math flaw of our random number multiplied by five
so we're getting 0343, four, two, one.
So I am getting five random numbers.
The problem I have now is that it's going from zero, right?
Our random numbers, just math at random in general goes from 0 to 1, not inclusive.
Then I multiply it by five.
I'm never actually going to have five as a random number.
I'm going to have zero up until four.
So if I want 1 to 5, I then need to shift everything up by adding one.
So this will give me a random number from 1 to 5.
Now what if I wanted to go from 1 to 10?
Well, I just multiply this by ten.
Now I have random numbers between one and ten random integers.
What if I want to go from 20 to 22?
Well, all I need to do is figure out how many options, how many numbers am I trying to generate?
Well, 20, 21 and 22.
That's three numbers.
And then I can shift this upwards by 20.
So I get 20, 21, 22.
So not that common to do that.
It's most I'd say, most common to generate a number starting from either zero to something or one to
something.
But if you ever need a range, you can shift it upwards as much as needed.
So that is math at random.
On its own, it gives us a random decimal from 0 to 1.
We can transform that.
And to use math for to help us get integers where we don't have that crazy looking decimal.
So we have other methods on the math object right round absolute value power which will raise something
to a power.
Although we already know how to do that, we don't really need the math object.
We can do this.
Math.
Power of, say, two to the third is the same as just two star star three.
So if you have any other questions about the math object, take a look at MTN.
Remember, we have not really covered objects at all, so all that we know right now is that an object
is just a place to store stuff, so it's not all hanging out on its own in the middle of nowhere.
All this math functionality has been combined into a single object, a single entity called math, with
a capital M.