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

Is this good? I was confused so i looked at other answers for inspiration.

var min = parseInt(prompt("Enter min number first")); var max = parseInt(prompt("Enter max numb now"));

function randoNum(min, max) { var random = Math.floor(Math.random() * (max - min + 1)) + min; return document.write("your random number is - " + random); }

randoNum(min, max);

1 Answer

document.write() doesn't return anything so your function doesn't return anything so it is kind of an odd choice for a return statement. It makes more sense to me that your function would return variable random with document.write() coming after the function like this:

var min = parseInt(prompt("Enter min number first"));
var max = parseInt(prompt("Enter max numb now"));

function randoNum(min, max) {
  var random = Math.floor(Math.random() * (max - min + 1)) + min;
  return random; 
}

document.write("your random number is - " + randoNum(min, max));

Could you tell why is it necessary to add +1 please? Or why this formula works like this, kinda complicated.

Math.floor(Math.random() * (max - min + 1)) + min;