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 trialNelson J
7,411 PointsFeedback: 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
231,271 PointsYou 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!
Willemijn B
5,577 PointsQuestion: 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
231,271 PointsThe 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
5,577 PointsI 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.
Nelson J
7,411 PointsNelson J
7,411 PointsThank you Steven Parker +100