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 trialJason Hill
Full Stack JavaScript Techdegree Student 11,399 PointsConfused on second part of the random generator between two numbers.
So I struggled with this one a bit. First I was confused as how to get the Math.random function to generate a random number between 1 and your input, if it only generates 0-1 but not including 1. Then I saw the multiply by 100 + 1, and that made some sense but I did not see how as a newbie, I was supposed to know to start there.
Understanding that, I thought I had the second part done, but then it generated numbers below my lowest number out of the two. What I struggle to grasp here is how I was supposed to know to tell Math.random to take topNumber - bottomNumber + 1, then + bottomNumber. I understand what he’s doing at first, telling it to pick a number between the two to multiple Math.random to, and then add + bottomNumber.
I guess I don’t see how it actually works? If I picked 32 and 64. It would take 32 - 64, which would leave you negative. How is that then supposed to generate a number between 32 and 64? I think I may be overthinking this, and definitely am missing a concept here. Just from the start I had no clue to do anything he is doing here. I feel like I’m lost and wouldn’t have ever known how to get here. Anyone shed some light on this for me?
discouraged
1 Answer
Steven Parker
231,236 PointsThe "topNumber" should always be the highest one. You should not have a negative multiplier.
The formulas used to do things like this are more about math than programming. Most programmers don't pull things like this out of their head but look them up in a reference source. Then the programmer's task is to just implement the formula in code.
Jason Hill
Full Stack JavaScript Techdegree Student 11,399 PointsJason Hill
Full Stack JavaScript Techdegree Student 11,399 PointsYes that’s what I figured. I knew I was over thinking as I tend to do that. I was just wondering if I’m missing anything. But since were asking them for numbers, via prompt. What if they enter a smaller number as topNumber, thus leaving a larger as bottom and causing a negative? Thank you for you’re reply.
Steven Parker
231,236 PointsSteven Parker
231,236 PointsYou can swap the order, if needed, between the inputs and the formula:
OR you can alter the formula so the order doesn't matter:
OR you could use a loop to require to user to repeat the inputs until they put them in the right order.
Happy coding!