Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
216,121 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
216,121 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".