Stop Low Resolution Warning Sign

Transcript

Transcript of the tutorial. TOP


          
        
          All right then.

          So hopefully now you're pretty comfortable with creating objects and knowing what objects are because
          
          they're a big part of the JavaScript language.
          
          And in fact JavaScript has a whole bunch of ready made objects built into the language which we can
          
          use out of the box.
          
          And one of those is the math object.
          
          Now this math object has got several properties and methods all pre-made and bundled into it.
          
          So we access the math object just by saying math with a capital M.
          
          Oops, I've done that the wrong way round.
          
          Math with a capital M, So let me just log this object to the console like that and save it.
          
          And over here we can see this math object and we can see all of these different properties and all of
          
          these different methods attached to the math object that we can use.
          
          So, for example, if I come to the next line, I'm going to say console dot log math dot pi and pi
          
          is in capital letters.
          
          Now Pi is a mathematical constant 3.14 and we can access it this way, but it's going to give us a longer
          
          version of that number with all of these different decimals.
          
          So it's more accurate.
          
          So that's one number we can use of the math object.
          
          We'll do another console dot log, then math dot E, and this is Ula's number, another mathematical
          
          constant.
          
          And we can see that right here as well.
          
          So we can use these different constants down here and we can see them all listed, but we can also use
          
          different methods.
          
          So let's just have a look at a few of those.
          
          First of all, what I'm going to do is create a constant called area, and I'm just going to set it
          
          equal to 7.7.
          
          Now, underneath, I'm going to say console dot log, then I'm going to use the math object and I'm
          
          going to use a method called round and take in the area as a parameter.
          
          So what round does is round the number to the nearest integer, so either up or down depending on this
          
          decimal.
          
          If it's nearer to eight, then it's going to round it up.
          
          If it's nearer to seven, it rounds it down.
          
          So if I save this we should see eight.
          
          Now if I change this to 7.2, we should see seven because it rounds it down.
          
          Okay.
          
          So that's one function we can use.
          
          Let's do another.
          
          I'm just going to duplicate this and I'm going to change this round method instead to floor.
          
          Now, what floor does is take a number like this and floor it to the number below what this is.
          
          So for example, no matter what this decimal is, it's going to floor it to seven because it's seven
          
          point something.
          
          If it was five point something, it would floor it to five.
          
          So if I save it, we can see seven.
          
          I'm going to change this to 4.7 and we should see four.
          
          Okay.
          
          Okay.
          
          So that's the floor method.
          
          Let's do another.
          
          I'm going to duplicate that and change this.
          
          Now to seal.
          
          And this does exactly the opposite of floor.
          
          It rounds it up.
          
          So regardless of what this is, it could be 7.1.
          
          It's going to round it up to eight because ceiling references the top.
          
          So we could change this to something like 5.3 and it's going to round it to six.
          
          Okay.
          
          So then let's do one more.
          
          I'm going to duplicate this again and this time I'm going to use a method called trunk.
          
          So if I save this now and preview this, we can see that this is five.
          
          So all it does is take away the decimal and leave the integer.
          
          Okay.
          
          So if we change this to 7.7 again, then we're going to see oops, not 7737.7.
          
          We're going to see that this is now seven.
          
          All right then.
          
          So there's a few different methods we can use on the math object.
          
          Now I want to show you one use case of the math object that I use quite frequently, and that is to
          
          generate random numbers.
          
          So we have access to a method on the math object called random.
          
          So I'm going to create a constant called random and set it equal to math dot random like this.
          
          Then I'm going to log that to the console.
          
          So console dot log random.
          
          So if I save this, we can see over here we get this random number and if I keep refreshing, it's going
          
          to change all the time because it's completely random and that random number is always between 0 and
          
          1.
          
          So it's a decimal number between 0 and 1.
          
          Now, what if we wanted a random number between, say, one and 100?
          
          Well, then we could do something like this.
          
          I'm going to say console dot log and I'm going to get math dot round taking the random constant that
          
          we have right here.
          
          So let's see what that is, first of all.
          
          So math dot round, remember and refresh.
          
          So that is either 1 or 0 all the time.
          
          If I keep refreshing, it's 1 or 0.
          
          Now, if I take that random number and times it by 100 in here, then it's going to be any number.
          
          Between 1 and 100.
          
          So if I refresh this, we can see 99, 14, 18, 26, 97, etcetera.
          
          So this is a nice way to get a random number between 1 and 100.
          
          So there we go.
          
          My friends, that is the math object is just one of JavaScript's built in objects that we can use.
          
          There's also other objects built into the language which we are going to use later on as well, such
          
          as the date object.