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

Vince Leung
Vince Leung
4,730 Points

Random number function

Did i put in the wrong formula or made a mistake on my parameters? I seem to be getting a number that isnt in the range of my minimum or maximum number.

Any help would be appreciated THANKS!

function calc(minN ,maxN){
 var randomN = Math.floor(Math.random() * (maxN - minN + 1)) + minN;  
 return randomN;
}

var lower = prompt("Please enter first minimum number");
var upper = prompt("Please enter second maximum number");
alert ("This is your new number " + calc(lower, upper));
Aaron Martone
Aaron Martone
3,290 Points

As an aside, there is no point in creating a variable in the function just to immediately return it in the next line. As in jcorum's example, you simply return the expression. It is evaluated first and then sent to return which returns the value, without the unnecessary need to create a variable (uses memory) since it is only scoped to the function and not available elsewhere.

5 Answers

Vince Leung
Vince Leung
4,730 Points

I appreciate it guys :) Thanks!

Vince Leung
Vince Leung
4,730 Points

For some reason, still spitting out wrong results. It's not producing a number between the two prompted numbers.

Not sure...

function calc(minN ,maxN){
 return Math.floor(Math.random() * (maxN - minN + 1) + minN);  
}

var lower = prompt("Please enter first minimum number");
var upper = prompt("Please enter second maximum number");
alert ("This is your new number " + calc(lower, upper));

Vince, I copied your code, pasted it into an online JavaScript tester (https://jsfiddle.net/) and ran it a dozen times. Each time I get a number between the min and max. You might want to try that, just to see if you get the same results.

Vince Leung
Vince Leung
4,730 Points

Hmm, it seemes like the formula was actually right. I don't know how your jsfiddle worked, because my codepen didnt work. The formula i had was actually correct as explained here: http://stackoverflow.com/questions/1527803/generating-random-whole-numbers-in-javascript-in-a-specific-range .

I'm not 100% sure sure if this was right solution but it worked... i change my prompt to have the parseInt(), as they were strings, so that could be reasoning i was getting these weird numbers.

Here is my solution and seems to be working: http://codepen.io/leungvi8/pen/NrPxZq

I appreciate your help jcorum :)!

Vince

Vince, you have one of the parentheses in the wrong place in your formula. Try this instead:

function calc(minN ,maxN){
 return Math.floor(Math.random() * (maxN - minN + 1) + minN);  
}