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
amit dilawri
1,244 PointsMy solution
var num1 = prompt('Welcome to the random number game....!!! \nEnter a first number greater than zero: '); num1 = Math.round(parseFloat(num1)); var num2 = prompt('Enter a second number greater than first number: '); num2 = Math.round(parseFloat(num2)); var randNum = Math.floor((Math.random() * (num2 - num1)) + 1); randNum = randNum + num1; alert('Resultant random number is ' + randNum + ' which is between the range of ' + num1 + ' and ' + num2 + ' . ');
1 Answer
Özgür Ozan Çakmak
11,451 PointsHello, For starters I'd styled up your code like thus. You should check the Markdown Cheatsheet for sending code by the way.
var num1 = prompt('Welcome to the random number game....!!! \nEnter a first number greater than zero: ');
num1 = Math.round(parseFloat(num1));
var num2 = prompt('Enter a second number greater than first number: ');
num2 = Math.round(parseFloat(num2));
var randNum = Math.floor((Math.random() * (num2 - num1)) + 1);
randNum = randNum + num1;
alert('Resultant random number is ' + randNum + ' which is between the range of ' + num1 + ' and ' + num2 + ' . ');
Secondly I don't think this code works correctly. Say, if I entered 20 to the num1 prompt and 25 to the num2. The end result will be a value between 1 and 5. I am not sure but this can be done like this:
var randNum = Math.floor(Math.random() * (num2-num1)+num1);
In this case the inner random function will give me something between 0 and 4 and add it to the 20, therefore I'll have something between 20 and 25.
Happy coding!
amit dilawri
1,244 Pointsamit dilawri
1,244 PointsThank you for your help!