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 Solution

Mirali Mirzayev
Mirali Mirzayev
1,980 Points

Solution for the last Challenge in Functions

Hello everyone, I just would like to get your guys opinions on my solution for the last challenge (number 2) for Functions. I figured it out this way before finding out that it was to give out an error anyways but i think i used my skills to come up with a way that still produces errors with an alert box. <CODE> function getRandomNumber( lower, upper ) { return Math.floor(Math.random() * (upper - lower + 1)) + lower; if(isNaN(upper)||isNaN(lower)){ throw new Error ("Error with the number"); }else{ return alert(getRandomNumber);

} } alert( getRandomNumber( 'nine', 24 ) ); alert( getRandomNumber( 1, 100 ) ); alert( getRandomNumber( 200, 'five hundred' ) ); alert( getRandomNumber( 1000, 20000 ) ); alert( getRandomNumber( 50, 100 ) ); </CODE>


I am not sure if anyone from the Tree house team can see this message and specially Mr. Dave, i would like to just give my sincere thanks for putting together such a complex material in such an understable fashion for a price of 3 or 4 tasty espressos a month. Before coming to Tree House i tried 5 different platforms (all of them gave me materials upon materials to prepare) I couldn't understand it all so i found some paces where it was one on one learning and i could have made a better sense of it all and then quickly found out that it was very expensive and what you learn for that price might or might not help you create a simple quiz (at least $4000 for 2 month just for the basics you guys teach here for $25 a month which $50 for two month): So i was disencouraged, but knew that there has to be a platform other than Khan, Solo, Udacity and others i tried where i can just understand a little bit to see a reason to continue because i really wanted to learn, then thanks for your adds on youTube, i check it out and its been 2 weeks: I am not great at it but i have far more understanding of everything i am learning on your platform than i did with other platform for 4 month prior. And it's fun.

I can't wait to see what's next.

Thank you TreeHouse

1 Answer

Mirali,

If you just want to print the error message on an alert window, just replace the "throw new Error..." for a simply alert message. Ex. alert("Error with the number");

I also found a small bug on your code. As we learned during this course, once a function returns a value it ignores the rest of code within the function. (a function can only return a single value). Therefore, I suggest you to move the "return Math.floor (Math.randon()..." line of code to inside the else {} if you want the error message to show up.

Thx!