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

Andy tran
Andy tran
7,112 Points

Question about the second challenge generating random numbers with negative number

In this video, if users input bottom number with the small value, like says 4, and top number with the big value, like says 6, it seems fine and generates accurate random numbers, 4, 5, 6. My question is what if users input bottom number with value 6 and top number with value 4? The result will be negative and the whole calculation will be false. Is there another way we can fix it?

var bottomNumber = prompt('Input bottom number!!!'); var topNumber= prompt('Input top number!!!'); var result = Math.floor( Math.random() * (parseInt(topNumber) - parseInt(bottomNumber) + 1))+ bottomNumber; document.write('<h2>' + ' Your random number is ' + result + ' between ' + bottomNumber + ' and ' + topNumber + '.</h2>');

1 Answer

Kieran Barker
Kieran Barker
15,028 Points

Hey Andy,

I'm not sure this is the most efficient way of doing this -- maybe someone else can comment on this -- but you could use a while loop to make the program keep asking for two numbers until the user makes the bottom number lower than the top number. Like this:

var input1 = prompt('Please type a starting number.');
var input2 = prompt('Please type a number.');
var bottomNumber = parseInt(input1);
var topNumber = parseInt(input2);

while (bottomNumber >= topNumber) {
  alert('Please make sure your first number is lower than your second number');
  var input1 = prompt('Please type a starting number.');
  var input2 = prompt('Please type a number.');
  var bottomNumber = parseInt(input1);
  var topNumber = parseInt(input2);
}

var randomNumber = Math.floor(Math.random() * topNumber - bottomNumber + 1) + bottomNumber;
var message = '<p>' + randomNumber + ' is a random number between ' + bottomNumber + ' and ' + topNumber + '.</p>';
document.write(message);

You'll learn about this in a later lesson!

Andy tran
Andy tran
7,112 Points

Thanks!! oh, how do I make my comment box background black like yours? It makes the codes so much easier to read for the readers. Many thanks!!!

Kieran Barker
Kieran Barker
15,028 Points

@andytran11 All you need to do is surround your code with three backtick (`) characters on either side. Make sure you use backticks and not regular apostrophes! And you’re welcome!