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!

5 posts were split to a new topic: Why can’t I use .isInteger() on a number itself?

2 posts were split to a new topic: How do I ensure the number is between 0 and 50?

14 posts were split to a new topic: How do Math.random() and Math.floor() work together?

2 posts were split to a new topic: What am I supposed to find from the documentation?

6 posts were split to a new topic: Can you help with this error?

I hope there are no more like this example:

Section 9:   
 question 1.  Inside of a console.log(), create a random number with Math.random(), then multiply it by 100.
    answer 1.  console.log(Math.floor(Math.random()) * 100);   is wrong but showed as correct
    answer 2.  console.log(Math.random() * 100);                       is correct and showed as correct
Both show as correct.  This was confusing for me.

A post was split to a new topic: Javascript should state inclusivity of Math.Random

I think I need a little more guidance on this section. I could not for the life of me realize how to even get the first line of code. Help!

1 Like

I followed the instructions exactly and even copy/paste the code in there and it still says something is wrong and just shows a 0 everytime instead of random number.

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

Did you use the Math.floor() method to wrap your random number generator? - I thought i did?

Consider what the methods do. Math.random() generates a number between 0 and 1. The location of the parenthesis matters. Math.floor() returns the next integer lower than the value passed to it.

> Math.floor(2.689)
2
> Math.floor(1.234)
1
> Math.floor(0.9913987)
0

So, Math.floor(Math.random() ) * 100 is equivalent to 0 * 100 which is always 0.

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

That’s not what we want. We want the randomly generated float that is between 0 and 1 to be multiplied by 100 before we perform the Math.floor() operation.

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

*Note: The float returned by Math.random() could be 0, but will never be 1. It is between 0 (inclusive) and 1 (exclusive).

1 Like

Thank you for the reply. I don’t understand methods (or anything really in JavaScript) at the moment. Should I learn that language before learning JavaScript? I thought integers were whole numbers? The numbers in the () are all decimals.

What Math.floor() essentially does is drops everything after the decimal point. It takes a float, and returns an integer. In my examples, the code following the > is input, and the line below is the output.
If you pass Math.floor() an integer, it will return that same integer.

So Math.floor() returns an integer lower as well as drop everything after the decimal? I wasn’t sure what the > was, thank you for clarifying. Is float like floating in CSS? Where do I learn about methods? the expercise has this link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String but I can’t find .trim or .math anywhere

Not as well as, that is part of flooring. We only want to the integer portion of the number.

Math.floor(1.99999999)  =>  1
           ^
        integer

No relation, whatever. In numbers, a float is any number with a decimal, even if followed by 0.

1    =>  integer
1.0  =>  float

In CSS, the term, ‘float’ comes from typography. The typographer would position a photo or image, then the text would fill all the space around it. The text floats around the image.

Math is a built-in JavaScript object. You can read about its methods here.
String is also a built-in object. You can find its methods (including .trim()) here.
Look at the far left column. Click the link called String.prototype.trim().

2 Likes

Thank you. I’m bookmarking these for reference. This is defiantly much harder than I would have ever thought.

2 Likes

Question: Do I need to know how to do Algebra or complex math in order to get a grasp on JavaScript? I’m… uh… for lack of a better term, am an abysmal mess when it comes to math. :upside_down_face:

Maths is a language, and like any language that is foreign to us, including programming languages will have a steep learning curve to begin with. Chances are that if it is easy then we are not being challenged, and likely won’t learn much. It should be hard. It is not insurmountable, though, and we don’t need to be rocket scientists or quantum mechanics mathematicians.

We get a tremendous leg up when we explore maths and sciences, especially physics independent of programming. It just depends how deeply we wish to get immersed in the study of data, information, or other scientific pursuits.

Even game development involves maths of varying degrees. The key is to know what sort of project we can take on given our level of understanding of maths. Most of us have by the age of 13 a basic level of maths, namely arithmetic. It is usually around that age we begin to study algebra and the more analytical side of geometry. Bear down and do the work. The learning will come. Set aside your fear (which is usually based on a false assumption) and pour yourself into it.

2 Likes

I have a question that is causing slight confusion within me:

The objects that we are taught are built into JS - such as console and Math, they aren’t following the same rules both being an object as far as I can tell. This may seem insignificant or the reason may be going over my head, but -

Why is the object ‘console’ executed with a lower case letter while the ‘Math’ object has an upper case letter? is there a specific rule to this that we should look out for?