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

Gong T
Gong T
1,891 Points

My Solution! Not sure, if I should of used 'const' instead of 'let' in the generate a random number line

// Collect input from a user
const number = prompt('Give me any number between 1 and 6! It could be in decimals!');

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


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


// Create a message displaying the random number
document.querySelector('main').innerHTML = `The number is: ${randomNumber} !`;

Please review and let me know what you guys think! I think I should of used const instead of let for the randomNumber variable. Thank you!

I think const would work better, since randomNumber won't be changing. Maybe let would work better if you coded a way to get a new randomNumber with a brand new input from the user?

Mark Casavantes
Mark Casavantes
2,880 Points

Hello Gong,

You cannot set the propery of 'innerHTML" to null.

2 Answers

Gong T
Gong T
1,891 Points

Hello Mark Casavantes! Thank you for taking the time to review my code. Could you explain why it can't be used to null? Thank you!

Mark Casavantes
Mark Casavantes
2,880 Points

Hello Gong,

I reviewed the lesson and see that consol.log is used. console.log(`The number is: ${randomNumber} !`);

I do not think 'main' references anything in your code. My understanding is that .innerHTML is used to change text in webpage. https://www.youtube.com/watch?v=SrSBhuuuIsg

I don't think you were wanting to change HTML text in your program. I hope this is helpful to you.

Gong T
Gong T
1,891 Points

Hi Mark! Thank you for your response. Okay, I understand now! You are right that this would be better in console.log.