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

Jason Welsh
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jason Welsh
Treehouse Project Reviewer

I am not understanding the second part of the Random Number Challenge.

I found a solution to the first part of the challenge easily, but could not find a solution to the second part, and decided to just watch the solution video. After watching it a few times, I still do not understand at all. Can someone try to explain it in a different way, please? It is very discouraging. Thanks in advance.

3 Answers

Here's an attempt to explain the solution step-by-step:

//create a variable and initialize it to the value provided by the user via the prompt
//E.g., if the user enters 23 then the value of input1 will be "23"
var input1 = prompt("Please type a starting number");

//convert the user input's to a number using parseInt(), and store it in a variable named bottomNumber
//prompts return Strings, so the input has to be converted to a number: 23
var bottomNumber = parseInt(input1);

//create a 2nd variable and initialize it to the value provided by the user via the prompt
//E.g., if the user enters 101 then the value of input will be "101"
var input = prompt("Please type an ending number");

//convert user input to number and store in topNumber
//after this step topNumber will be a number: 101
var topNumber = parseInt(input);

//generate a random number between topNumber and bottom Number using the 
//Math.floor() and Math.random() methods, and the formula (max - min + 1) + min
//So the variable randomNumber will be Math.floor(Math.random() * (101 - 23 + 1) + 23)
var randomNumber = Math.floor(Math.random() * (topNumber - bottomNumber + 1) + bottomNumber);

//create a String using concatenation (+) to put it all together into one message
var message = "<p>" + randomNumber + " is a number between " + bottomNumber + " and " + topNumber + ".</p>";

//write the message to the page
document.write(message);

Let me know if any of the steps need further explanation.

As Dave pointed out, there are no edits, so there will be problems if the user enters text instead of a number. The output (message) will be something like this: NaN is a number between NaN and NaN.

Edited to correct a typo.

Tushar Singh
PLUS
Tushar Singh
Courses Plus Student 8,692 Points

And what if someone adds a bigger number in the first prompt? for eg. 1st number is 20 and the other 10??

Then what?:o

Then the formula would be

Math.floor(Math.random() * (10 - 20 + 1) + 20)
Daniel Garwood
Daniel Garwood
711 Points

//E.g., if the user enters 112 then the value of input will be "101"

How did you get 101 from 112? Is this a typo?

Daniel, good eyes! Thank you for pointing out my error. I've edited my response to correct it. Not sure what happened -- certainly doesn't look like just a typo.