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

analyn jane calado
analyn jane calado
3,523 Points

how to make a random number.

i need help on how to make it..

2 Answers

geoffrey
geoffrey
28,736 Points

To achieve this you'll often use Math.random() & Math.floor() functions. An exemples:

var nbr1 = Math.floor((Math.random() * 10) + 1); //Return a random number between 1 and 10:
var nrb2 =  Math.floor((Math.random() * 100) + 1); // Return a random number between 1 and 100;

Hope this helps.

Garrett Carver
Garrett Carver
14,681 Points

here's how I did it:

var userNum = prompt("Enter a number");
var rando = (Math.random() * parseInt(userNum)) + 1;
rando = Math.floor(rando);
alert(rando);