Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialseattler
2,067 PointsDave M, used Math.floor(Math.random()*6) vs. parseInt(Math.random()*6) to mimic a 6 sided dice. Both produce same rght?
I know this lesson was meant to expand awareness of other methods. I was just curious if there was an advantage to using one method or the other.
Math.floor(Math.random()*6) // converts decimal values to integers
parseInt(Math.random()*6) // coverts the randomly generated # to integer.
I'm not sure if this questions makes sense but I'm seeking to better understand both of these methods.
1 Answer
miikis
44,957 PointsHey Trish,
Even though both those expressions produce the same output, the one Dave used is more ... semantic, I think is the word.
parseInt()'s primary function is to convert String data types into Integer datatypes. It was not meant to round a number down to the nearest Integer. However, Math.floor()'s explicit purpose is to round a number down to the nearest Integer.
It's really a question of "appropriateness" — JavaScript is quirky enough without trying to use native functions to do other native function's jobs.