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 JavaScript Basics (Retired) Working With Numbers The Random Challenge Solution

Why Math.random doesn't have any input in the function parentheses

In the code solution, for my code, I put the user's input within the Math.random() parentheses (so it would look like Math.random(/user input/)

Why do you not put the user's input within the Math.random function?

1 Answer

Samuel Webb
Samuel Webb
25,370 Points

Because Math.random() doesn't need an argument. What you'd want to do is Math.random * userInput. Math.random() returns a random number between 0 and 1. So you want to multiplay it by whatever the user's input is. You could also do Math.floor(Math.random() * userInput) or `Math.ceil(Math.random() * userInput)' to get a whole number (integer).

What would the code be saying if I typed Math.random(userInput)?

Samuel Webb
Samuel Webb
25,370 Points

It won't say anything. It'll just give you the random number it would have given you regardless.

Ok thx

Samuel Webb
Samuel Webb
25,370 Points

To learn more about Math in JS, check out the Documentation Here