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, Part II

Enemuo Felix
Enemuo Felix
1,895 Points

I can't print randomNumber to my document

My code seem to stuck at the prompt stage , then after that Nothing else. How do I print it to document? Even the alert I added to the code couldn't run

function getRandomNumber( lower, upper ) { 
if (isNaN(lower) && isNaN(upper)) {
throw new Error ("Arguement is not a Number!");
} else {
return Math.floor(Math.random()* (upper-lower+1)+lower);
}
}
var firstN = parseInt(prompt("Enter your first lower Number"));
var secondN = parseInt(prompt("Enter your Second Upper Number"));
var calcRandom = getRandomNumber(lower,upper);
alert (calcRandom);

Steven Parker

2 Answers

Steven Parker
Steven Parker
231,269 Points

:bell: Hi, I got the alert from your tag.

The prompts are storing the input values in "firstN" and "secondN", but when getRandomNumber is called, it is passed arguments named "lower" and "upper" which have not been defined. So the call needs to pass the establshed variables instead:

var calcRandom = getRandomNumber(firstN, secondN);

Also, I noticed that the validity test only detects when both values are bad using AND logic. It would probably be better to detect if either value was bad using OR logic:

if (isNaN(lower) || isNaN(upper)) {
Enemuo Felix
Enemuo Felix
1,895 Points

I did as you Suggested by using the OR logic and ran the code but the throw new error doesn't run when i deliberately input a string in the prompt box(although I don't know if it's suppose to pop an alert or write) but it's not doing anything.

function getRandomNumber( lower, upper ) { 
if (isNaN(lower) || isNaN(upper)) {
throw in Error ("Arguement is not a Number!");
} else {
return Math.floor(Math.random()* (upper-lower+1)+lower);
}
}
var firstN = parseInt(prompt("Enter your first lower Number"));
var secondN = parseInt(prompt("Enter your Second Upper Number"));
var calcRandom = getRandomNumber(secondN,firstN);
alert (calcRandom);

Steven Parker

Enemuo Felix
Enemuo Felix
1,895 Points

Thank you very much for your help Steve. I'm grateful