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) Creating Reusable Code with Functions Random Number Challenge Solution

Thiago Reis
Thiago Reis
2,247 Points

Here's my solution!

function random (a, b) {
  var result = Math.floor(Math.random() *  + b + 1);
  if (result >= a){
  alert(result);
  } else {(random();)
    }
 }

var numberOne = prompt('Type a low number');
var numberTwo = prompt('Type a big number');
random(numberOne, numberTwo);

2 Answers

Check your link. It doesn't work.

If you need random number between "a" and "b" you know "space" between them: "b - a". Next you should choose randomly something and summarize it with lower number: random() * (b-a) + a. But this way is "exclusive" case of getting "b".

We need to add 1 to make out function "inclusive" maximal(big) value also:

function random (a, b) {
  var result = Math.floor(Math.random() * ( b - a + 1) + a);

The next part is strange. I don't remember task, but you try to insure that random number is not... bigger then or equal lower one? It's always will be greater or equal by code you do.

  if (result >= a) {
    alert(result);

This part will never happens, but if it will both "a" and "b" is undefined, because you have no parameters inside random function. And the parenthesis is useless also.

  } else {
    (random();)
  }
}

And this part is ok.

var numberOne = prompt('Type a low number');
var numberTwo = prompt('Type a big number');
random(numberOne, numberTwo);