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

This is not actually a question but more a remark. According to the Random Number Challenge by Dave McFarland

Hi Dave,

I tried your solution of the random numbers challenge by myself several times. But how can I fix the bug that happens when the bottomNumber exceeds the topNumber - then I get a negative randomNumber as a result and a statement that say something like:

   " -17 is an number between 98 and 3".
Savannah Lynn
Savannah Lynn
13,662 Points

Hi Sabine,

I ran into something similar when I was going through these lessons. In real life, I would tell users to enter a number larger than the first number. You could create an else if clause such as : else if user's first number is less than user's second number, display alert "Try again! I need a number larger than first number" or something similar.

5 Answers

Seth Kroger
Seth Kroger
56,414 Points

There is a function to get the absolute value of a number Math.abs().

Another way to handle it is to check whether bottomNumber is greater than topNumber and flip the two values.

Yes Savannah - i was also thinking about squaring the resulting random number and taking the square root again (cause there exist no negative squares ;-) ) . But I don´t know yet how to accomplish such thing and/or if this is a good idea.

Savannah Lynn
Savannah Lynn
13,662 Points

math.sqrt() will return the square root of a number https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sqrt and math.exp() will allow you to square it

arik
arik
5,791 Points

I am not sure, please correct me if I am wrong. But I think it will not result in negative number even though the bottomNumber exceeds the topNumber, since at the end it will be added with the bottomNumber. Therefore it will always return positive values. example : 0.99999* (1 -1000000 +1) + 1000000 will result positive value. I took 0.999999 as the highest possible value from random number generated by Math.random() function to gives the highest negative result in the first operation before it is being added to bottomNumber. And since the bottomNumber is exceeding the topNumber it will result positive value.

Once again, please correct me if I am wrong...

Lia Seltene
Lia Seltene
2,641 Points

I tested that a couple times too but wondered if that would make the highest number generated fall short of the intended range.

I know the math isn't the intended learning part but I wasn't able to solve the second challenge because I didn't know how to get there via math. I guess in real life I would google it, but I can see why having a solid understanding in maths/algorithms would be important for programmers, or at least in progressing in their career/understanding.

nico dev
nico dev
20,364 Points

I guess he explained in the last part of the video that weird things can happen, and that in order to make this work better, he will teach "all about conditional statements" in the coming videos.

I can only imagine this example was not intended to be used in actual coding, but only a nice, funny way for us to play and learn the random, math, and other concepts. Granted, if you click OK accidentally, for example, it won't work so wonderfully, will it? :)

But we shouldn't expect such precision so early in our learning anyway. We should have fun and play around to get familiar with the concepts. Once that's done, true stuff will become "easier" for us, or at least, I should say, we will feel more comfortable with it.

Looking forward to learning further in the coming videos. See you there!

nico dev
nico dev
20,364 Points

Sorrry, I forgot smth.

Plus: like Savannah said (and I quote): "In real life, I would tell users to enter a number larger than the first number."

Completely agreed. That's indeed what I did.

Like this:

var user1stNumber = parseInt(prompt("Please type a number higher than 1"));
var user2ndNumber = parseInt(prompt("Please now type another number, now higher than " + user1stNumber));