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 trialYoan Halevy
Full Stack JavaScript Techdegree Student 2,964 PointsThe code I have gives me back a value of "undefined". Can someone tell me why?
const randomNumber = (lower, upper) => {
Math.floor(Math.random() * (upper - lower + 1)) + lower;
if (isNaN(lower) || isNaN(upper)) {
throw Error ("You must select only numbers");
}
}
// Call the function and pass it different values
console.log(randomNumber(1,5));
console.log(`${randomNumber(5,100)} is a random number between 5 and 100`);
console.log(randomNumber("six",987324));
1 Answer
Joseph Yhu
PHP Development Techdegree Graduate 48,638 PointsYou have to return the random number, like this:
const randomNumber = (lower, upper) => {
if (isNaN(lower) || isNaN(upper)) {
throw Error ("You must select only numbers");
}
return Math.floor(Math.random() * (upper - lower + 1)) + lower;
}