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 trialMD MONIRUZZAMAN
6,130 PointsAdding 1 inside
What is the difference between
const dieRoll = Math.floor(Math.random()*6) +1;
const dieRoll = Math.floor(Math.random()*6 +1 );
2 Answers
Steven Parker
231,248 PointsSince the "floor" function only affects the fractional part of a value, it makes no difference if a whole number is added before or after. So those two statements will perform exactly the same.
Scott Hert
2,364 PointsTo answer the question from Zachery Smith, if you use Math.ceil it will round 0.000001 to 1 and then add 1 making 2 the lowest possible roll, and round anything greater the 5.0 to 6. Then add 1 making 7.
boi
14,242 PointsGood one Scott!
Zachary Smith
Full Stack JavaScript Techdegree Graduate 16,616 PointsZachary Smith
Full Stack JavaScript Techdegree Graduate 16,616 PointsOne thing I still am not clear on is why in the video, he says not to use Math.ceil for the die example because it could return a zero. How is this possible? Doesn't "Math.random()" return a number between 0 and 1? Even if the random number is .000001, that would express to a 1, would it not? I've tried referring to the notes on Mozilla which state "The Math.ceil() function always rounds a number up to the next largest whole number or integer.". I guess I'm missing something....
EDIT: Disregard, it seems I missed the part where he says it could gen a number from zero up to, but not including 1.