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

Feedback: Generate a random number challenge

var lower = parseInt(prompt("enter first number"));
var upper = parseInt(prompt("Enter second number"));

function randomNum(lower, upper) {
  console.log("random number is " + Math.floor(Math.random() * (upper - lower + 1)) + lower); 

}


randomNum(lower, upper);

Also, can you add in if I need parseInt. I'm confused on that a little.

3 Answers

Steven Parker
Steven Parker
231,269 Points

You have used "parseInt" correctly. Since "prompt" returns a string, the "parseInt" converts that into a number, which is what your "randNum" function expects as arguments.

Good job! :+1:

Thank you Steven Parker +100

Willemijn B
Willemijn B
5,577 Points

Question: are the prompts at the top necessary at all, given that you'll pass values as arguments when calling the function (and therefore not using the numbers entered when prompted)? And if that's correct, can you use the prompted numbers by nesting them into the function?

Steven Parker
Steven Parker
231,269 Points

The prompts are necessary because that's how the values are brought in that are passed as arguments when calling the function.

I'm not sure what you mean by "use the prompted numbers by nesting them into the function" — can you shown an example?

Willemijn B
Willemijn B
5,577 Points

I understand now! I was confused because I was actually creating variables within the function, rather than using the global ones supplied before. Thanks for your quick response, Steven.