What does Math.floor() do? How do I use it?

Question

What does Math.floor() do? How do I use it?

Answer

Math.floor(number) will return the largest whole number (integer) that is less than or equal to the number passed to the Math.floor function.

Example usage:

Math.floor(8.7124); //will return the number 8
Math.floor(147/9); //will return the number 16

The code below (the solution for the exercise - Fix The Broken Code) seems to be valid and working; however, when I want to console.log(die1), it returns " ReferenceError: die1 is not defined at Object. ". Any ideas why?

const rollTheDice = () => {
let die1 = Math.floor(Math.random() * 6 + 1)
let die2 = Math.floor(Math.random() * 6 + 1)
return die1 + die2
}

1 Like

die1 and die2 have a local scope, they only exist within the function.

9 Likes

Hello,

I would like to know if it is possible to have this code instead:

const rollTheDice = () => {
let die1 = Math.floor(Math.random() * 7)
let die2 = Math.floor(Math.random() * 7)
return die1 + die2

?

Thx in advance!

That wont work because Math.floor(Math.random()*7) will return a random number between 0 and 6, 0 inclusive.

1 Like

In mathematical terms, think of random as a linear equation…

r = ax + b

where x = { x | 0 <= x < 1; x is Real }; and,
where a = { a |  a != 0; a is Real or Integer }; and,
where b = { b |  b is Real or Integer }.

That expresses the full range of possibilities of a random number, whether positive or negative. We can narrow it down by using only integers for a and b, and making a positive.

b can be negative or positive since it is only a shift in the y axis but has no bearing on a * x. This works great when we have a set midpoint and want to echo above or below that midpoint.

Relating this to this question, x is the Math.random() expression; a is the number we multiply by, namely the number of sides of the die; and b is the offset. Since we don’t want zero, and we want 6 then an offset of 1 will shift the numbers from, [0..5] to [1..6].

let dice = [
  Math.floor(Math.random() * 6 + 1),
  Math.floor(Math.random() * 6 + 1)
];
return dice[0] + dice[1];
9 Likes

I want to ask why this code always renders variables die1 and die2 as “1”… why do I have to to put whole (Math.random()) * 6 + 1 expression in parentheses ?

const rollTheDice = () => {

let die1 = Math.floor(Math.random()) * 6 + 1
let die2 = Math.floor(Math.random()) * 6 + 1
console.log(die1)
console.log(die2)
return die1 + die2

}
console.log(rollTheDice())

you don’t have to, you could do:

let temp = Math.random() * 6 +  1

this will give you a random float between 1 and 6 (inclusive) (so for example: 5.4432453898093)

then you could floor this:

Math.floor(temp)

the problem with your code is that first a random number between 0 and 1 (exclusive) is generated, that is floored (rounded down to zero), multiplied by 6 (which is zero), then one is added which will always give one

1 Like

Thank You!! I get it now :slight_smile:

Won’t it be better if we used Math.round instead of Math.floor + 1 ?

let die1 = Math.round(Math.random() * 6)

vs

let die1 = Math.floor(Math.random() * 6 + 1)
1 Like

Equal result but one less operator. You could say this is more efficient since it is less code and costs less effort for the computer ;).

1 Like

That will result in there being seven values in the outcome set since round can go upward, not just downward.

die1 = { x | 0 <= x <= 6; x is int } => {0, 1, 2, 3, 4, 5, 6}

die2 = { x | 0 < x <= 6; x is int } => {1, 2, 3, 4, 5, 6}
4 Likes

Ah, you would have to use Math.ceil then :smiley:

This will still give the same solution set as round. The ceiling of zero is zero.

4 Likes

I didn’t like the 6 + 1 at the end, so I was wondering if this code is ok:
const rollTheDice = () => {
let die1 = Math.ceil(Math.random() * 6);
let die2 = Math.ceil(Math.random() * 6);
return die1 + die2;

1 Like

No, Math.random() can also generate 0, ceil(0) is still zero, so your range is not correct.

1 Like

Why is it that console.log(die1) gives me a error message? It says that die1 is undefined. Any ideas?

Can we see your full code?

Why tell us to log it into the console if it comes up as a error?

This is mine. I am having the same issue. I understand there is an error because die is in the function as a local code…but why tell us to log it then? I don’t see the point.