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

My Random Challenge Solution - Thoughts?

alert ('Hello.  I will now predict your lucky number for today.');
var number1 = prompt ('Please give me a number.');
number1 = parseInt(number1);
var number2 = prompt ('Please give me another number.');
number2 = parseInt(number2);
var randomNumber = Math.floor (Math.random () * number1 * number2) + 1;
alert ('Your lucky number today is . . . '+ randomNumber);

This is my first time posting a question with code, so just in case I did it wrong, here is the link.

https://w.trhou.se/27k2du8sfa

Thanks! S

1 Answer

Steven Parker
Steven Parker
229,785 Points

You didn't provide a link to the page, but typically the objective would be generate a random value that is between the supplied numbers. The formula to do that is:

Math.floor(Math.random() * (highlimit - lowlimit + 1)) + lowlimit;

Then, you'd either need some extra code to identify which of the numbers was higher, or you could just change the prompts to specifically ask for a higher number and a lower one.

The line of code I used was:

var randomNumber = Math.floor (Math.random () * number1 * number2) + 1;

And, I find that it seems to work just fine for this challenge. I don't know how or why I decided to throw the multiplication signs in there rather than the subtraction signs, I think at that point I was just confused and thought that it would work. To get a little bit better understanding of how the randomNumber works, I was doing my own bit of research on it, so maybe that's how I confused myself. However, I've been testing it out in the work space preview and it seems to work just fine, like I said. It doesn't even matter which order I type the numbers in when the dialogue box pops up; least to greatest, or greatest to least. Both ways work.

I guess I'm just confused a little bit by why it works? Especially since I didn't add extra code to identify which of the numbers was higher or lower.

I know that

Math.floor(Math.random() * (highlimit - lowlimit + 1)) + lowlimit;

is the normal line of code for this challenge, but do you have any insight to how my code works?

Thanks for getting back to me so quickly!

Steven Parker
Steven Parker
229,785 Points

It doesn't really "work", you're probably just testing this with "lucky data" (numbers that don't reveal the issues).

Try generating a few numbers between 10 and 20. Then try generating some between 100 and 110. It should become clear that the results don't stay between the provided values with this formula. And if either of the two numbers is "0" the output will not be random at all, it will always be "1".