Built-in Objects(Math objects)

 INTRODUCTION TO JAVASCRIPT

In addition to console, there are other objects built into JavaScript. Down the line, you’ll build your own objects, but for now these “built-in” objects are full of useful functionality.

For example, if you wanted to perform more complex mathematical operations than arithmetic, JavaScript has the built-in Math object.

The great thing about objects is that they have methods! Let’s call the .random() method from the built-in Math object:

console.log(Math.random()); // Prints a random number between 0 and 1

In the example above, we called the .random() method by appending the object name with the dot operator, the name of the method, and opening and closing parentheses. This method returns a random number between 0 (inclusive) and 1 (exclusive).

To generate a random number between 0 and 50, we could multiply this result by 50, like so:

Math.random() * 50;

The example above will likely evaluate to a decimal. To ensure the answer is a whole number, we can take advantage of another useful Math method called Math.floor().

Math.floor() takes a decimal number, and rounds down to the nearest whole number. You can use Math.floor() to round down a random number like this:

Math.floor(Math.random() * 50);

In this case:

  1. Math.random() generates a random number between 0 and 1.
  2. We then multiply that number by 50, so now we have a number between 0 and 50.
  3. Then, Math.floor() rounds the number down to the nearest whole number.

If you wanted to see the number printed to the terminal, you would still need to use a console.log() statement:

console.log(Math.floor(Math.random() * 50)); // Prints a random whole number between 0 and 50

To see all of the properties and methods on the Math object, take a look at the documentation here.


Instructions:

1.

Inside of a console.log(), create a random number with Math.random(), then multiply it by 100.

Hint:

You can generate a random number between 0 and 100 with:

Math.random() * 100

Since Math.random() will generate a number between 0 and 1, multiplying it by 100 will make the result between 0 and 100.


2.

Now, use Math.floor() to make the output a whole number.

Inside the console.log() you wrote in the last step, put the existing Math.random() * 100 code inside the parentheses of Math.floor().

Hint:

The code inside the parentheses of your console.log() should look like:

Math.floor(Math.random() * 100);

By placing Math.random() * 100 inside of the parentheses of Math.floor(), we can make sure that the number generated will be a whole number. For instance, if Math.random() * 100 generated the number 64.65, Math.floor() would make the result 64.

Note: Be careful of parentheses here. Each opening parenthesis should have a closing parenthesis.

Also, remember that you’re using the console.log() statement from the previous step to print out the result of your random number which has been rounded down!


Solution:

console.log(Math.floor(Math.random()*100));


3.

Find a method on the JavaScript Math object that returns the smallest integer greater than or equal to a decimal number.

Use this method with the number 43.8. Log the answer to the console.

Hint:

Use the .ceil() method to calculate the smallest integer greater than or equal to 43.8.


Solution:

console.log(Math.ceil(43.8));



4.Use the JavaScript documentation to find a method on the built-in Number object that checks if a number is an integer.

Put the number 2017 in the parentheses of the method and use console.log() to print the result.

Hint:

Use the .isInteger() method from the Number object to check if 2017 is an integer.

See an example of how to use .isInteger() and log the result:

console.log(Number.isInteger(34.2)); // Prints false

Solution:

console.log(Number.isInteger(2017));


Comments