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

Why is my code rendering 0?

https://w.trhou.se/h8wytwa0eg

here is my workspace. Not sure why I get 0 for a random number?

I am adding something between the () of Math.random... it isn't right, but why?

Thanks for your help!

1 Answer

Math.random() doesn't take an argument. It generates a number between 0 (inclusive) and 1 (exclusive). You then take the floor of this number which will always be 0.

The proper use to generate a random number is to multiply Math.random() times something. This for example will generate a random number between 1 and 10

Math.floor((Math.random() * 10) + 1);

Okey thanks!