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

daniel zaragoza
daniel zaragoza
1,843 Points

Am I doing it right??

Please critique and let me know if this is right thanks. Here is my code.

var userNum = prompt ('type number');
var randomNum= parseInt(Math.random() * userNum);
alert('This is your number ' + randomNum + userNum);

1 Answer

hello Daniel Zaragoza, the challenge states : "See if you can build a program that collects a number from a user, then prints out a random number from 1 to the number the user selects."

I have copied your code in to a workspace and tested it in the browser and it seems as if your code is including the 'userNum' value within the random number instead of using it to create a random number. The random number is being generated and concatenated with the value which the user inputs in to the prompt and it is indeed lower than this value...so you are nearly there.

You want to try and use the Math.floor function to pass a lower number than the one that the user inputs:

var userNum = prompt ('type number');
alert('This is your number ' + Math.floor(Math.random() * parseInt(userNum)));

Hopefully you will see this post before you watch Dave's solution video and this will help with your challenge.

daniel zaragoza
daniel zaragoza
1,843 Points

Hello Jade Newbury thanks for the help. I'm gald to know that at least i was on the right path, by the way, I haven't watch Dave's solution yet because i want to make sure i'm always pushing myself to the limit until i have no more answer in me that im left with no choice but to look at the solution. Once again thank you.