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 Review Working with Numbers

var myNumber = prompt('write your number'); var top = parseInt(myNumber); var randomNumber = Math.floor(Math.random()

this code is not working properly. i don't know why?

1 Answer

Kevin Gates
Kevin Gates
15,052 Points

Hi there,

I'm using the Markdown CheatSheet to make your code easier to read:

var myNumber = prompt('write your number');
var top = parseInt(myNumber);
var randomNumber = Math.floor(Math.random()

If you look above, you are missing the closing parenthesis and semicolon for your Math.floor function.

Should be:

var randomNumber = Math.floor(Math.random());

code is like this var myNumber = prompt('write your number'); var top = parseInt(myNumber); var randomNumber = Math.floor(Math.random() * top) + 1; var message = '<p>' + randomNumber + ' is a number between 1 and ' + top + '. </p>'; document.write(message);

now its not working properly, i am getting result but that's not what i expected

Kevin Gates
Kevin Gates
15,052 Points

Hi there! For your future responses, if you look below the text input field, you will see a reference to a Markdown Cheatsheet. It mentions there how you can add markdown so your code becomes easily readable.

You have this (I added notes as comments):

var myNumber = prompt('write your number');  // So let's say someone says 10, it'll be 10 as a string, like: "10"
var top = parseInt(myNumber); //This parses: "10" to become 10.
var randomNumber = Math.floor(Math.random() * top) + 1; //This finds a random number between 0 and .9999ff, then times by 10, then finds the floor (0-9), then adds 1, so 1-10.
var message = '<p>' + randomNumber + ' is a number between 1 and ' + top + '. </p>'; document.write(message);

Which part is confusing for you?