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

random number becomes NaN in console

Please help, after putting in the low number console will correct display the lowNumber and highNumber, but the randomNumber always comes out NaN

// Collect input from a user

const lowNumber = prompt ('low number');
const highNumber = prompt ('high number');


// Convert the input to a number

const newLowNumber = parseInt(lowNumber);
const newHighNumber = parseInt(highNumber);

// Use Math.random() and the user's number to generate a random number

const randomNumber = Math.floor( newHighNumber * Math.round())+ 1 - newLowNumber ;

// Create a message displaying the random number

console.log (`${randomNumber} is a number between ${lowNumber} and ${highNumber}`);

3 Answers

Steven Parker
Steven Parker
231,153 Points

Math.round() without any argument will return NaN, and then any calculations done with NaN will also return NaN.

Hint: you don't need to use Math.round for this, but the formula needs some other adjustments as well.

Fran ADP
Fran ADP
6,304 Points

That means that there is an error.

Thanks you Steven. That must be a typo - should be Math.random (forgive me it was 5am in my time zone yesterday)

Was not targeting the default questions provided just trying to do something slightly different