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

adrien kovanic
adrien kovanic
5,727 Points

Would this answer work?

Hi, I am doing the JavaScript JavaScript Basics Creating Reusable Code with Functions Random Number Challenge, Part II would this solution also be correct? ( I am not checking if the arguments are number I know)

thanks a lot :)

function calculus (lowerNumber, higherNumber){ var result = Math.floor(Math.random() * (higherNumber)) + 1;

if (result > lowerNumber){ alert("the number is" + result); } else { var minresult = result + lowerNumber; alert("the number is" + minresult); } } console.log(calculus(100 , 200));

2 Answers

Steven Parker
Steven Parker
229,744 Points

This code will produce numbers outside the range.

Try calling it with a narrow range of larger numbers, like: calculus(950, 999) a few times and see. Plus even if it was modified to constrain the answers to the range, the distribution would be uneven.

And wasn't the whole point of Challenge Part II to test the arguments for validity?

If you call the function after the block of codes, I think it will work.