About "Math.floor" and "Math.random"


#1

Hi guys, I’m striving to take my first steps in learning code and I’m put to a halt because of this exercise dealing with Math.floor and Math.random.

The instructions state
"Now, utilize Math.floor to make the output a whole number.
Inside the console.log you wrote in the last step, put Math.random() * 100 inside the parentheses of Math.floor."

I can only guess that the formatting of the code is wrong. Pls help.
https://www.codecademy.com/en/courses/learn-javascript-introduction/lessons/introduction-to-javascript/exercises/libraries?course_redirect=introduction-to-javascript

My code gives me a number, but without the number being rounded down (e.g: 93.89349777904413)



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



#2

you got things in the wrong order, Math.random() generates are (pseudo)random number between 0 and 1, then you multiply this with 100 to generate a random number 0 and 100, then floor it to get a whole number

you first multiply Math.floor() (which is a function call, so it requires parentheses) with 100, then generate a random number


#3

So how would the finalized line look like if you instead first multiplied Math.floor with 100, then generated a number? I’m not sure if I follow you.


#4

why would you do this? You first want to generate a random number, which function is responsible for this task? Math.random() obviously, the name is a pretty big give away

after you generated the random number you want to floor (round down) the number, which function could achieve this?


#5

Err maybe I read things wrong but I thought in your first reply you switched the order. Got it now, thanks.


#6

i did switch the order? You got them in the order wrong order, so i had to switch them to get them right


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.