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 challenge : I don't see any error from the console log but am I correct with solution like this?

// Collect input from a user const myLowNumber = prompt('Please enter your lowest number.'); const myHighNumber = prompt('Please enter your highest number.');

// Convert the input to a number const LowestNumber = parseInt(myLowNumber); const HighestNumber = parseInt(myHighNumber)

if (HighestNumber && LowestNumber) {

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

const randomNumber = Math.floor( Math.random() * (HighestNumber - LowestNumber + 1) + LowestNumber);

// Create a message displaying the random number

let myMessage = document.querySelector('main').innerHTML = <h2>${randomNumber} is a random number between ${LowestNumber} to ${HighestNumber}.</h2>;

} else { alert('Please enter a number. Try again!'); }

1 Answer

Steven Parker
Steven Parker
231,141 Points

I see two issues:

  • a logic "&&" alone is not a good test for number validity (try generating a random number from 0 to 9!)
  • the creation of the "myMessage" variable is not needed

And when posting code, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.