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 trialIvan Kusakovic
12,197 PointsIs this also correct with NaN?
function getRandom(a,b){
if (isNaN(a||b)){
throw new Error("there is smth what's not a number");
}else{
return Math.floor(Math.random() * (a - b +1 ) +b);
}
}
alert(getRandom(100,2));
alert(getRandom(6,0));
alert(getRandom(1030,255));
alert(getRandom("ivan",222));
alert(getRandom(100,50));
This (isNaN(a||b)) works also.
6 Answers
sizwengubane
15,244 PointsHello :) I messed around with your code a little bit and i came up with this
function getRandomNumber(min,max){
if(isNaN(min) || isNaN(max)){
throw new Error("Please enter numbers only!");
}else {
return Math.floor(Math.random() * (min - max) + 1) + max;
}
}
You can call that function and check..it works fine the function u wrote is a bit confusing..this is more simple yet performs the same task as your function thanks
Brian Mendez
7,734 PointsI can see how you thought that worked. Either way, good thinking though Ivan.
Ivan Kusakovic
12,197 PointsWhy write longer if you can shorter? I can't see you made much different with messing with my code. Messing :)
IsNaN(a||b)
is quite understandable so I recommend this :)
sizwengubane
15,244 Pointsit doesn't work bro you have to give the full conditions on either side
Ivan Kusakovic
12,197 PointsIt works. I tried and works. It actually compare if is not a number a or b ... isNaN(a||b) compare if isNan(true) a||b is true if either of a or b is true so that works.
Try and you'll see it ;)
Casey Ydenberg
15,622 PointsisNaN(1||'casey') // returns false
isNaN('casey'||1) // returns true
Remember that b || c
returns b is b is truthy and c if b is falsey; so in your code, isNaN is evaluating either the first argument or the second but not both of them.
Ivan Kusakovic
12,197 PointsAh, yeah. Sorry, I was little bit confused because I typed as example right choice :D
Thanks Casey :)