JavaScript JavaScript Basics Creating Reusable Code with Functions Random Number Challenge

Nelson J
Nelson J
5,984 Points

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
176,805 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:

Willemijn B
Willemijn B
5,573 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
176,805 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,573 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.