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 trialJamie Davies
2,787 PointsIS this an OK solution?
const lower = parseInt(prompt('Type in a low number.'));
const upper = parseInt(prompt('Type in a high number.'));
const getRandomNumber = (lower, upper) => {
if (isNaN(lower && upper)) {
throw Error("This isn't a number reload the page and start again");
}
return Math.floor(Math.random() * (upper - lower + 1)) + lower;
}
document.write(`Your low number is ${lower} and your high number is ${upper}.<br>
A random number between both of these is ${getRandomNumber(lower, upper)}`);
1 Answer
rydavim
18,814 PointsLooks pretty good, nice job!
I would note is that you're not catching the edge case if someone puts in one number and one string.
if (isNaN(lower && upper)) { // Fails if both entries are strings, but not if only one is.
throw Error("This isn't a number reload the page and start again");
}
You'll want to check to see if the lower number or the upper number aren't integers.
The other very minor thing is that it's possible to get a random number that is the same as the upper number. I don't know whether you intend this or not, but it's something to investigate if you wanted an extra challenge.
But you're doing great, keep up the good work and happy coding!
Jamie Davies
2,787 PointsJamie Davies
2,787 PointsThanks for your feedback, much appreciated!
When using this code it throws an error if both are strings and if only one of the inputs is a string which is what is intended in the challenge i thought. I have tried to use the OR
||
operator and that allows the function to run with one input as a string or I am not grapsing something here?No i never intended for that to happen but i will have a look into it and see if I can do that as an extra challenge :)
Thanks
Kirt Perez
7,374 PointsKirt Perez
7,374 PointsJamie Davies, having one argument as a string going into that if statement would not throw the error.