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 trial

JavaScript Treehouse Club - MASH MASH - JavaScript Changing Stuff in JavaScript

pawkan kenluecha
pawkan kenluecha
26,303 Points

what do we put 4 in random_number() function ? Array start from 0-3 (4 items) If random number is 1, it's gonna be 4.

I still don't understand the random_number() function. Why do we have to pass 4 as parameter ? Because array start from 0. According to the original code, there're 4 items. The index should be 0 1 2 3. If we get 1 from math.random , we would get 4 as number for array index. Will this cause a bug ?

1 Answer

Mark Pryce
Mark Pryce
8,804 Points

I can't remember exactly without a refresher run but from what I do remember the math.random will pick a random number between 0 and 1, example: 0.99 now if your random number is 3, 0.99 * 3 = 2.97.

When you apply math.floor it will round down to 2

Now although you're right and it ranges from 0,1,2,3 with the above equation you can never return 3.

0.99 * 4 = 3.96 (apply math.floor) you will return 3 (the last in your array)

Hope this helps, I know it might have a little fault but I'm sure I'm on the right track.

So if you add another item to your list but keep the random_number at 4 you can never return the last item you listed unless you change it to 5.

pawkan kenluecha
pawkan kenluecha
26,303 Points

Ok, I see. Thanks. I forgot about Math.floor function. Then, 0.1*4 should be 0.4 , It'll round down to 0. 0.9*4 should be 3.6, It'll round down to 3.