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 Numbers The Math Object Random Number Challenge – Solution

NaN returned for variables when trying to document.write

My code worked fine to console.log I tried using document.write and the response returns NaN for both variables. Using console typeof(random) returns "number"

When I set it back to console.log, both variables report NaN in statement. I must have changed something but I can't tell what. Am I missing something obvious?

// Collect input from a user
const input = prompt('Please provide a number.');

// Convert the input to a number
const number = parseInt(input);

// Use Math.random() and the user's number to generate a random number
const random = Math.floor( Math.random() * number ) +1;

// Create a message displaying the random number
console.log(`${random} is a number between 1 and ${number}.`);

1 Answer

Never mind. I was using my keypad on my keyboard and the 'numberlock' had been disengaged so when I thought I was inputing a number, I was actually just hitting 'enter.'