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

Tammy Chang
Tammy Chang
2,447 Points

Is my code correct / Math.floor() versus parseInt() for Random Number Challenge

Hello -

Instead of using Math.floor() like the instructor, I used parseInt(). I know that they are different, but in this challenge, could I use either one of them?

Also, my code is shorter than the instructor's code. I've run it a couple of times and it seems to work. Am I doing it right ( even though I used pareseInt instead of Math.floor)?

var firstNumber = prompt("Give me a number.");

var secondNumber = prompt("Give me a second number.");

var randomNumberGenerator = parseInt(Math.random() * (secondNumber - firstNumber + 1) + firstNumber);

var sentence= randomNumberGenerator + " is a number between " + firstNumber + " and " + secondNumber + ".";

document.write(sentence);

1 Answer

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

Yes, it does work... for certain input. But it implements bugs for others. So your shorter code would, in fact, not be shorter if you had to include code to check the input.

I encourage you to try it and see what happens when you try to roll a number between -10 and 0 :sparkles: