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

Zach Meissner
Zach Meissner
1,347 Points

Is this still a valid solution to completing this exercise?

var userNumber = prompt('Give me a number');
document.write(parseInt (Math.random() * userNumber)) +1;

3 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Zach,

The only issues I see are.

  1. You have parseInt in the wrong spot, it doesn't need to be wrapped around Math.random() as that's already a valid number
  2. you don't have the closing parenthesis after your +1 addition
var userNumber = prompt('Give me a number');
document.write(Math.random() * parseInt(userNumber) +1);

Also for all times sake if you don't have a huge floating pointing value you can use Math.floor() to remove everything after the decimal point.

var userNumber = prompt('Give me a number');
document.write(Math.floor(Math.random() * parseInt(userNumber) +1));

Happy coding!

Hey Zach,

The range for this solution always has a minimum of 0 and a maximum of userNumber. And you cannot add a number to a document.write() statement. It would need to be done within the document.write() statement and not outside of it.

But, the actual solution for this exercise is to allow the user to define the minimum and maximum range for the random numbers, which requires more than one input. So you would need to define variables that get the user's maximum and minimum ranges as Dave has done. Doing it the way Dave does is the best method.