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

I would like to check my answer. Is it necessary to even dictate the lower number, based on my code?

var firstNumber = parseInt(prompt("Pick A Number!"));
var theRandomNumber = Math.floor(Math.random() * firstNumber) + 1;
document.write("<h1>" + theRandomNumber + "</h1>");
document.write("<p> We picked a number between 1 and " + firstNumber + ".</p>");

//Fixed Code Presentation

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

If you didn't have the + 1 it would produce a number between 0 and the firstNumber - 1 because Math.random() produces a number between 0 and 1 but doesn't include one. IE it could roll .9999999 but not 1. So if your firstNumber was equal to 6 and you're multiplying Math.random() by 6... it'll never equal six and will always round down. The end result would be between 0 and 5 instead of between 1 and 6. Hope that clarifies things!