Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Functions Arrow Functions Testing for Number Arguments

Zydrunas Likas
Zydrunas Likas
3,517 Points

My solution for Function Challenge Solution any advice welcome

alert ("Ready to find any random number between your any numbers??");
const param2 = +prompt("Please provide smalest Number?");
const param1 = +prompt("Now its time for bigger Number?");


const randomNumber = (param1, param2) => {
const randomNumberbetween = Math.floor(Math.random() * (param1 - param2 + 1 )) + param2;
if (isNaN(param1) || isNaN(param2)) {
return alert("You choose not a numbers!!!");
} else if ( param1 <= param2) {
return alert("You choose not correct Numbers!!!")
} else {
return alert(`Your Random Number Is ${randomNumberbetween} between ${param1} and ${param2}!!!`);
}
} 

randomNumber(param1, param2);

1 Answer

Steven Parker
Steven Parker
216,863 Points

Since you're doing validity checking on the arguments, you might want to perform the calculation only after the arguments are determined valid.

Also, using the unary "+" to convert a string to a number can cause unexpected results. For example, if the user submits a blank line, it will be converted into 0; but you might prefer it to be considered a "not a number" response, which is what you would get by converting with parseInt().

But in general, good job!   :+1: