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

marie king
marie king
1,172 Points

For some reason I'm getting NaN as my number. I'm not sure why when I'm typing in integers for my test number.

I copied him basically because I had no idea how to do it.

var input = prompt('Please type a number');
var topNumber = parseInt(input);
var randomNumber = Math.floor(Math.random * topNumber) + 1;
var message = '<p>' + randomNumber + ' is a number between 1 and ' + topNumber + '</p>';
document.write(message);

MOD: I just updated the question to format the code to make it more legible. See the Markdown Cheatsheet for how to do this. Thanks!

1 Answer

Tabatha Trahan
Tabatha Trahan
21,422 Points

It looks like the issue might be with how you called the Math.random function. Add the opening and closing parentheses and try it again. Your var randomNumber declaration would look like this:

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

When you call a function, just remember to include the parentheses. Some functions will take arguments, like the Math.floor, and some don't, like Math.random. I hope this helps.