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!

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 Basics (Retired) Working With Numbers The Random Challenge Solution

Sam Kautz
Sam Kautz
Full Stack JavaScript Techdegree Student 3,194 Points

My code seemed too simple compared to the solution. Am I thinking about this wrong?

Hey, guys. Can you please take a look and see if what I wrote is going to cause some issues? It seemed simple but after reviewing the Solution I feel like my code is over simplified?

var lownumber = prompt('Enter a low number!');

var highnumber = prompt('Enter a high number!');

var randNumber = Math.floor(Math.random() * (highnumber - lownumber +1));

var message = (Math.floor(randNumber));

document.write('A number between ' + lownumber + ' and ' + highnumber + ' is ' + message + '!!!');

2 Answers

Steven Parker
Steven Parker
228,039 Points

You're very close, but it looks like you forgot to add the low number back in when implementing the standard formula. And you should convert the inputs from strings to numbers rather than depending on implicit type coercion.

And you don't need to perform the "floor" operation a second time.

Fix those, and you'll essentially have the classic solution.

Veselin Radosavljevic
Veselin Radosavljevic
3,863 Points

and he will get a string instead of a number coz var lownumber is a string ..