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 Solution

Emanuel Cabanga
Emanuel Cabanga
5,775 Points

The 1 in the method

I made without that number 1 up there, it is working normally, but i would like to know why is it there?

3 Answers

hello emmanuel lets see an example

function randomNum() {
  return Math.floor(Math.random) * 5 );
}

// if you run this function you will get always get a number between 0 and 4 but never 5 

in the next example we call the same function but we add + 1 at the end

function randomNum() {
  return Math.floor(Math.random) * 5 ) + 1;
}
// this will always give you a number between 1 and 5

or if you want to get the same result you simply multiply it by 6 instead of 5

function randomNum() {
  return Math.floor(Math.random) * 6 );
}
// this will give you the same result as the second example
// a number between 1 and 5 but never 6

hope this helps

Harald N
Harald N
15,843 Points

Hi Emanuel. It would be helpful if you could tell more about your code and the your question. If you have the original code, it would be real helpful if you could post that too.

Emanuel Cabanga
Emanuel Cabanga
5,775 Points

// i made like this

var output = Math.floor(Math.random()*(number2-number))+number;

// but the original is like this var output = Math.floor(Math.random()*(number2-number+1))+number;

// there is that number 1, i would like to know what is he doing?