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
Molly Robinson
770 PointsDoes the program KNOW which number is the max and which is the min, or do I have to tell it?
To put it another way, when he writes
var randomNumber = Math.floor(Math.random() * (topNumber - bottomNumber + 1 )) + bottomNumber;
is there a step where we are supposed to ensure that "topNumber" is a larger number than "bottomNumber," or is that arbitrary? will it work out even if "topNumber" is smaller?
This really got me when I was trying to do the second challenge on my own. I felt I HAD to make sure the program KNEW which number was the largest and which was the smallest.
But, aside from the way he (the teacher) labels the keywords for the numbers, I'm honestly unsure if they represent a real (maybe necessary) value difference or not. (topNumber having to always be the larger number and bottomNumber having to always be the smaller number).
1 Answer
Jennifer Nordell
Treehouse TeacherHi there! You haven't included the link to the video here, but if I'm not mistaken elsewhere in the program the max and the min are either set or sent in as arguments to a function where this line is found.
In some scenarios, you will know in advance what the minimum number is and the maximum number. Let's say for example a 6 sided die. You know the minimum will always be 1 and the maximum will always be 6.
But what if you don't know? What if you want the user to choose the minimum and maximum? There are a couple of ways to handle this. Either you could keep prompting the user for the minimum and maximum and directing them to put a minimum number that is lower than the maximum or you could do something a little different. You could get the absolute value of the difference. For example, if they put in a minimum of 1 and a maximum of 6, then it would work as expected and the max minus the min would be equal to 5.
Users are wildly unpredictable. So if you got the absolute value and they put in a minimum of 6 and a maximum of 1, then the difference between max minus min would be -5. But the absolute value of either 5 or -5 is still 5. You could potentially just ask the user to put in two arbitrary numbers and get the absolute value of it and take out the guess work and error messages all together.
You can find documentation on the abs method here.
Hope this helps!
Matthew Long
28,407 PointsMatthew Long
28,407 PointstopNumberandbottomNumbermust be defined. As you have them they are referencing variables that you have not defined and thereforerandomNumberis undefined. Without settingtopNumberandbottomNumbersuch asvar topNumber = 4;andvar bottomNumber = 1;, for example, then you will receive an error.