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.
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.
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).
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().
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.
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.
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?