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

Molly Robinson
Molly Robinson
770 Points

if topNumber and bottomNumber must be defined why isn't it that the prompts reflect that?

I asked a question related to this earlier but I realized I needed to be more specific in my question. If "topNumber" the phrase does nothing on its own - if it's just a name for a variable - then how can we guarantee that the number imput ( by the user who read the prompt) will be the larger number? or the bottomNumber will be the smaller number?

1 Answer

Matthew Long
Matthew Long
28,407 Points

Without telling the program in some way what the topNumber and bottomNumber are it won't know. You can do this in many ways. Some better than other. You could use Math.min and Math.max` for example.

var numOne = prompt('enter a number'); // user types 2, for example
var numTwo = prompt('enter another number'); // user types 5, for example

var topNumber = Math.max(numOne, numTwo); // top number, or max number, is found to be 5
var bottomNumber = Math.min(numOne, numTwo); // bottom number, or min number, is found to be 2

var randomNumber = Math.floor(Math.random() * (topNumber - bottomNumber + 1 )) + bottomNumber;
Molly Robinson
Molly Robinson
770 Points

I understand, but then why doesn't the teacher go over that? Of course we are the "user" so we have control to say the first number we input is the larger number but if was another person, and they imput (min, max) instead of (max, min) or the opposite if whatever order they need to go in, will the program not run the right way? I feel like im overthinking this maybe, but its really bugging me. I guess I really just want to know whether he (the teacher) needs to establish value difference (find the min and max) for the specific problem he's doing (challenge two) or if he left that out because it doesn't matter in this specific case (and why).

Matthew Long
Matthew Long
28,407 Points

It's probably because Dave doesn't want to throw too much at the student at once. It is good that you're thinking ahead!

The learning structure on Treehouse is that you start with the absolute basics, like what this video is talking about, and as we build on the basics we get into more complicated things that is used in the real world. Some students can handle, and retain, more information at once than others. Since these are recorded videos Treehouse has to find a middle ground.

In the real world it is ideal to anticipate what a user may do, or the input a program may receive. Error testing really helps with this!