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 Functions Arrow Functions Testing for Number Arguments

how to have the same code but use prompt to pass numbers through the function?

function getRandomNumber(lower, higher) {
if ( isNaN(lower) || isNaN (higher)) {
throw Error("Both arguments must be a number.");
} else {
var randomNumber = Math.floor(Math.random() * (higher - lower +1)) +lower;
return randomNumber;
}
}

console.log(The random number is ${getRandomNumber(1, 5)});
console.log(The random number is ${getRandomNumber(10, 30)});
console.log(The random number is ${getRandomNumber(100, 300)});
console.log(The random number is ${getRandomNumber(1000, 3000)});
console.log("MESSAGE:- below is to test for an error; ten isn't defined! ");
console.log(This is to test for an error ${getRandomNumber(ten, 50)});

1 Answer

Hint: You can use "prompt" method to take ask for the input. and then use 'parseInt' so as to convert from String to Number.

Solution: function getRandomNumber() {

let lower = parseInt(prompt("Enter Lower Number")); let higher = parseInt(prompt("Enter Higher Number"));

if (isNaN(lower) || isNaN(higher)) { throw Error("Both arguments must be a number."); } else { var randomNumber = Math.floor(Math.random() * (higher - lower + 1)) + lower; return randomNumber; } } console.log(getRandomNumber());

Yes I did use prompt but was leaving out parseInt, thank you! :-)