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

What Am I Doing Wrong Here?

The Random Challenge

Here are the instructions:

Create a Random Number Generator. Collect a number from a user with the prompt command. Print a random number from 1 to the users provided number. Use paserInt to convert the input to an integer.

I am not receiving a message as this is a challenge performed via the launched workspace.

Here is what I have so far:

var guestSelection = prompt('Please enter a single digit number?');

var topNumber = parseInt(guestSelection);

var randomNumber = Math.floor(Math.random() * topNumber) + 1;

var message = '<p> 3 is a number between 1 and 6.</p>';```

 And the read out that I am getting is simply a page titled Random Number Generator. It gives the option to place an entry within the prompt dialog box. Then nothing else happens.

What am I doing wrong? 

Thanks in advance for any assistance provided!

1 Answer

Antony .
Antony .
2,824 Points

Hello, Michelle Dobbs

Technically to print this out to the page all you would need to do is use document.write() with the variable message inside the parentheses. But to simplify your code challenge I have changed a few stuff in your code to make it better.

var guestSelection = prompt('Please enter a single digit number?');

var topNumber = parseInt(guestSelection);

var randomNumber = Math.floor(Math.random() * topNumber) + 1;

var message = '<p> ' + randomNumber + ' is a number between 1 and ' + topNumber;

document.write(message);

You can also simplify further:

var userNumber = prompt('Give me a number');
var randomness = Math.floor(Math.random() * parseInt(userNumber));
alert(randomness);
Antony .
Antony .
2,824 Points

Looking good Casey!