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

Check 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! :-))

oh ok now i understand. I really feel happy when my code works or when i understand something Thanks

var startingNumber = parseInt(prompt("Enter a starting number"));
var endingNumber = parseInt(prompt("Enter an ending number"));
var random = Math.floor(Math.random() * (endingNumber - startingNumber + 1)) + startingNumber;
alert("A Random number between " + startingNumber + " and " + endingNumber + " is " + random); 

2 Answers

Steven Parker
Steven Parker
231,268 Points

You'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

First 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
Steven Parker
231,268 Points

If a user enters a 5 and a 10, the random number should be in that range (from 5 to 10).

Reza Zandi
seal-mask
.a{fill-rule:evenodd;}techdegree
Reza Zandi
Full Stack JavaScript Techdegree Student 1,599 Points

Hey 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'

Thanks Reza

Steven Parker
Steven Parker
231,268 Points

That 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.