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 Create the Loop – One Solution

Am I missing something, or is the script wrong in the example?

I'm super tired, so it's possible that I'm missing something but in the example (and thus in the workspace provided for the challenge too) the random number generator looks like this:

const randomNumber = getRandomNumber(10);


function getRandomNumber(upper) {
  return Math.floor( Math.random() * upper) + 1; 

which always provides the value given to the function - so in this case the "random" number will always be 10. (At least for me it came back as such and looking at the equatation it looks like it should do just that.)

When however I change it to:

const randomNumber = getRandomNumber(10);

function getRandomNumber(upper) {
  return Math.floor( Math.random() * upper - 1 ) + 1;

It provides a number between 1 and 10.

1 Answer

Hi Zoltán,

So in the first function you provide, the random number returned should NOT ALWAYS be the argument passed into the function. So if 10 is passed in, 10 should not be the number returned each time the function is called.

So here’s how the math should work (from inside out). Math.random() returns a random floating point number between 0 and 1 (not including 1). So it’ll return a number such as 0.786. Math.floor() then rounds that number down. So 0.786 would become 0.7. Multiplying that number by 10 gives you 7. Then adding 1 gives you 8. And that is the number that’s returned. Adding 1 just makes sure that the number returned can actually be the integer passed into the function.

The second function would do the same thing. The only difference is that by subtracting 1 the function should never be able to return the integer that’s passed into the function.

I’m not sure why either function would give you the same integer each time. The only reasoning I can find for this would be the fact that the function is called with the declaration of a variable. So if you’re doing something like making several console.logs where you’re logging randomNumber to the console, then that would give you the same number each time (as the value of randomNumber is not the getRandomNumber function, but rather the number returned from that function the one and only time it was called). But if you’re actually calling getRandomNumber(10) several times AND the number 10 is returned each time then it would seem that there is some kind of bug somewhere in the file. Because that should not happen.