Question in a practice pack

Hi everyone!

I am doing the quiz in introduction section and need help to understand the answer to the following question:
What will the following code log to the console?

let totalCost = 24.57;
console.log(Math.floor(totalCost));

The correct answer is 24, however, remembering from math, anything over .50 (such as .51) should be rounded to the nearest number, which in this case is 25. If it was .49, then it would round it of to 24. So, WHY the quiz says correct answer is 24?

Hi,

That’s what Math.round() does. In Javascript it is very useful to be able to control if you get the nearest bigger or the nearest smaller number. That’s what Math.floor() (always chops off the floats) and Math.ceil() (always rounds up to the next absolute number) are for.

I actually use Math.floor() much more often than Math.round() because the common use cases very often differ from the use case you know from math class.

1 Like

Thank you for the explanation mirja_t!

1 Like