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

Michael Humenick
Michael Humenick
1,138 Points

I don't understand what I'm doing wrong!!!

This is the code I did and its just also showing numbers below the ones I type in. Can someone please help me?

This is the code:

var min = prompt ('Tell me a number');

var max = prompt ('Tell me another number');

alert('Now I will form a random number from ' + min + ' to ' + max);

var number = Math.floor (Math.random() * min + 1);

document.write('The number is ' + number);

1 Answer

Steven Parker
Steven Parker
229,645 Points

The "prompt" function reads in a string, so if the answer is a number that you want to do math on, convert it to an int:

var min = parseInt(prompt("Tell me a number"));  // use "parseInt" to convert string to int

Then, the formula being used isn't making use of both inputs. The common formula used to create a random number between two limits is this:

var number = Math.floor(Math.random() * (max - min + 1)) + min;
Michael Humenick
Michael Humenick
1,138 Points

Steven! Thank you so much! It worked :)