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 Foundations Numbers The Math Object

Something wrong with the task (JavaScript / Math Object)

I'm on the stage 3 (Numbers) of JS course. And there is a challenge task (Challenge task 2 of 4).

The task is:

Create a variable named "chance" and assign a random number between 0 and 20 to it using the Math.random() method.

i type my answer:

var chance = Math.random(0,20);

but i've got the error message:

Bummer! Please use the Math.random() method in your answer.

Do not know what is wrong ...

2 Answers

Stone Preston
Stone Preston
42,016 Points

the Math.random() method does not accept any arguments. It generates a random number between 0 (inclusive) and 1 (exclusive). if you wanted a number between 0 and 20, you multiple the return value of Math.random by 20

var chance = Math.random() * 20;

it looks like this is explained in the previous video around the 2:38 mark or so.

I see. Thanks. Forgot to check out manual before asking... just get confused due to random() function in php.