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

Daniel McFarlin
Daniel McFarlin
5,168 Points

Random Number code isn't working and I can't find the mistake :(

Hey friends, I have looked over and over and over again trying to find where I made a mistake in this code to generate a random number between given numbers, but cant seem to find it. If anyone could point out what most likely is an obvious mistake looking at me right in the face, that would be much appreciated! Please and thank you!

My Code is:

//Bottom Number var input1 = prompt("Please type a starting number"); var bottomNumber = parseInt(input1);

//Top Number var input = prompt("Please type a number"); var topNumber = parseInt(input);

//Random Number Generated var randomNumber = Math.floor(Math.random() * (topNumber - bottomNumber + 1)) + bottomNumber; var message = "<p>" + randomNumber + " is a number between " bottomNumber + " and " + topNumber + "</p>"; document.write(message);

2 Answers

Hello Daniel McFarlin,

Here is the solution as followed

<script>
// You forgot a + between " is a number between " and  bottomNumber
var message = "<p>" + randomNumber + " is a number between " + bottomNumber + " and " + topNumber + "</p>";
</script>

Hopefully that helped!

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

you're missing a plus here

" is a number between " + bottomNumber

before bottomNumber. now it will work, but also it is recommended to always supply the second argument to parseInt(), which is the radix or base, usually 10.