FAQ: Introduction to JavaScript - Built-in Objects

This community-built FAQ covers the “Built-in Objects” exercise from the lesson "Introduction to JavaScript ".

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise Built-in Objects

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Why is “console.log(Number.isInteger(2017));” used for determining whether 2017 is a whole number, and not "console.log(2017.isInteger()) ?

I think its because your calling the function or whatever, or what you want to do with it, first. Its kinda like how yoda talks. “to the garbage, take this item” or something like that. so instead of is 2017 an integer, its “is an integer, 2017?”…I think

1 Like

When you’re using a method on an object (in this case the isInteger method on the number object)- Do I always have to refer to the general Object first? (Here: Number.) As in, does it apply to methods on other objects? Also I’m wondering why this doesn’t work (without “Number”): console.log(isInteger(2017)) …?

This section talks about using Math.floor(Math.random()*50); to get a number between 0 and 50.
But Math.random generates a number in the range 0–1 (inclusive of 0, but not 1) and
Math.floor rounds DOWN to the nearest whole number. So… to get a number that is between 0 and 50, wouldn’t we actually need to multiply by 51?

Hello!
I’m currently taking the web development path and I’m stuck on the third part, Javascript-Built in objects. I don´t understand the Math.floor part, can anyone help me?

console.log(Math.random()*100); ( this is the first part)
Math.floor(Math.random)() *: ( this is the second part) - how can I make it a whole number?

Regards,
Regina

1 Like

Math.foor() makes it a whole number. When you want a random number (integer) the standard syntax is:

Math.floor(Math.random() * n)) //n would be the number of possible options starting
//from 0. If n was 5 the pool of random numbers would be: 0, 1, 2, 3, 4.

Here’s what happens:
First, Math.random() generates a random number between 0 and 1. Sounds strange, I know, but that’s how it works. The possibilities start at 0 and go up to, but do not include 1. I ran Math.random() 5 times logging the results to the console:

0.39994290364154983
0.28103878884356925
0.11820979926770026
0.12778879681889976
0.6060919344582774

Back to our random number syntax: Math.floor(Math.random() * n));
After Math.random() generates a floating point random number, that number is multiplied by n.
If n were 5 and the random floating point number generated were 0.6060919344582774 that result is: 3.0304596722913866.
Finally, Math.floor() comes in. The Math.random()*5 returned 3.0304596722913866, so we have Math.floor(3.0304596722913866). Math.floor()returns the largest integer less than or equal to a given number’ - quote from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor.
In this case Math.floor(3.0304596722913866) returns 3, so the random number generated by Math.floor(Math.random() * 5)) is 3 in this instance. Since Math.random() cannot generate a number >= 1, the highest possible result in our example is 4.99999999999999999. Math.floor(4.99999999999999999) will return 4.
Hope this helps!

2 Likes

Difference between:
console.log(Math.floor(Math.random()*4 +1))

and

console.log(Math.floor(Math.random()*4 )+1)

I ran both and second one tend to be more constant(not so random) than the first one.
Are they same for 1~4?

There is no difference. The result of the evaluated expression will be the same either way you type it. Math.random() is going to return a floating point decimal between 0 and 1 (not including 1). That number will be multiplied by 4. This will result in a random floating point number between 0 and 3.9999999999999999.

In your first example, you then add 1 to the float making it between 1 and 4.9999999999999999. Math.floor() will then convert this float to highest integer that is less than the float. (Rounds down to the nearest whole number). That would give you 1, 2, 3 or 4.

In your second example, Math.floor() will convert the float returned by Math.random() to an integer value of 0, 1, 2 or 3, and then you add the 1, so you still end up with 1, 2, 3 or 4.

The randomness isn’t affected either way. Remember that for the result to be truly random, it is entirely possible to get the same number repeatedly. Every time Math.random() runs the chance for any outcome is the same as the chance for any other outcome.

This lesson doesn’t explain how to use the object with the console. I’ve tried every combination of parenthesis I can think of?
console.log(Math.floor(Math.random() * 100));
Get an error every time
Also, the lesson forgets that this is a beginners course, completely loses the plot by the 3rd instruction, and is incredibly poorly worded.

the smallest integer greater than or equal to a decimal number

This is quoted not only from the exercise text, but also from the page linked to in the instruction. We have only to peruse that page and we find those very words under, Math.ceil().

The ceiling of a number is always higher, unless that number is equivalent to an integer, such as 46.0. It’s ceiling will be 46. The ceiling of 46.0001 is 47.

just make it a habit that any time you use JavaScript do not forget to start with console.log
The Math lesson at hand does not do this and I too got a big red syntax error when I ran the string. I went back and added console.log to the front of the string and my return was red and error free

I’m new to JavaScript, but when it hits the instructions:

" 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."

This apparently explains how to do: console.log(Math.ceil(43.8));

Which I think it clearly doesn’t (Whats with the hint being .ceil rather than it actually being a part of an example, or part of the instructions itself?) Codecademy wants me to find my own method, but when I click the link I have no idea where to start. Then it says it wants specifically .ceil

As a complete noob it would be much better to see clearer instructions, as codecademy is my only resource for learning right now.

This would suggest scroll down through the definitions until you find the explanation that matches exactly the text given in the narrative.

The ceiling of 0 is 0. The ceiling of 14.0 is 14. The ceiling of 0.1 is 1 and ceiling of 14.1 is 15. Does that help the explanation sink in?

The problem am running into is this. The Math.floor command, where does it go. I have tried using it in every space possible and I am not getting it. Capture

The confusing aspect of this is that Math.random() does not take an argument, but Math.floor() requires one. The argument in this case is (Math.random() * 100).

Technically it can be written with multiplication outside.

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

Mathematically the result is the same.

This explanation may not be entirely correct. In case of String examples JS syntax is ‘my string’.toUpperCase() and not String.toUpperCase(‘my string’)

I hope someone can answer andreiradulescu question above.

Number.isInteger(n) is a Class Method, not an instance method, so is not inherited by numbers. We must address the class directly, and pass in an argument for the method to test.

Compare this to, Array.isArray(sequence), which is also a class method not inherited by array objects.

That is unlike, String.prototype.toLowerCase() which is an instance method inherited by all string objects.

myString.toLowerCase()

Notice there is no argument since the method acts upon the object in context.

1 Like