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

What could be the problem here?

My code refuse to run. Can someone point out the flaws here? My intention is to generate a random number and I chipped in the conditional statement to pop up an error if the user didn't input a number in both prompt.

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(lower,upper);
alert (calcRandom);

3 Answers

Patrik Horváth
Patrik Horváth
11,110 Points

Math.random () = Math.random() its function remove space betweeen m ()

if (isNaN(lower) && isNaN(upper)) - missing closing )

Just had a quick look and your IF statement is missing a closing ")"

Enemuo Felix
Enemuo Felix
1,895 Points

I have made the stated corrections but it still wont run.

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(lower,upper);
alert (calcRandom);

the answer may lie in this line of code

throw in Error ("Arguement is not a Number!");

I have never seen this way of throwing an error. Usually you would throw an error inside a try block such as

try {
if (isNaN(lower) && isNaN(upper)) {
throw "Argument is not a Number!";
} else {
return Math.floor(Math.random()* (upper-lower+1)+lower);
}
}
catch (err) {
// Do something with your error message
}
Enemuo Felix
Enemuo Felix
1,895 Points

Thanks Gary for your input. You are right! I think the problem is from that line. However i noticed it's suppose to be "throw new Error" and not "throw in Error". So I changed it and it ran. I noticed your disagreement with the way the Error function is used but Dave also used it this same way in his example.