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 trialSam johnson
Courses Plus Student 688 PointsCheck my code
Hi Is this ok?
var number;
var input1 = prompt("Enter a number");
var input2 = prompt("Enter a number");
if(input1 > input2)
number = input1 - input2;
else
number = input2 -input1
var random = parseInt(Math.random() * number) + 1;
alert("A Random number between 1 and " + number + " is " + random);
(MOD: I updated your question to add formatting to the code. That way it's easier for people to read and help you out. See the Markdown Cheatsheet below the text box when you're typing a question for how to do the formatting. Don't worry, it's easy! :-))
2 Answers
Steven Parker
231,236 PointsYou're getting close, perhaps a few hints will help with the rest:
- if you're working on the first task, you'll only need to input one number
- for the second task, you'll need a slightly different formula
- the "parseInt" should be applied to the inputs before doing calculations on them
- you might want to include "Math.floor" in the calculations to insure integer picks
Sam johnson
Courses Plus Student 688 PointsFirst of all Thank you for updating my question. Actually I am doing the second task. Anyways I saw Dave's solution and i am a little confused about the task. From my understanding a user has to enter two numbers say 10 and 5 , the difference between them is 5 i.e 10-5 , so a random number should be generated between 1 and 5. If i am wrong can you explain the second task
Thank you.
Steven Parker
231,236 PointsIf a user enters a 5 and a 10, the random number should be in that range (from 5 to 10).
Reza Zandi
Full Stack JavaScript Techdegree Student 1,599 PointsHey Sam, this is the documentation from MDN on getting a number between the range. Seems that the +1 is throwing things off in the second task because this is a different task.
function getRandomArbitrary(min, max) { return Math.random() * (max - min) + min; }
As you can see, there is no '+1' in this code, the 'min' replaces the '+1'
Sam johnson
Courses Plus Student 688 PointsThanks Reza
Steven Parker
231,236 PointsThat MDN example is for a different outcome. It creates a floating-point number up to but not including the "max".
The video formula generates only integer values, and including the max. That +1 is necessary to make it work, as is the "floor" function.
Sam johnson
Courses Plus Student 688 PointsSam johnson
Courses Plus Student 688 Pointsoh ok now i understand. I really feel happy when my code works or when i understand something Thanks