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

The solution code seems a lot more complex than mine.

My solution seems to work when I test it, but the challenge solution seems to be more complex. I'm worried my solution may not work in every scenario.

var userNumLow = parseInt(prompt('Select your low number'));
var userNumHigh = parseInt(prompt('Select your high number'));
document.write(parseInt(userNumHigh - (userNumHigh - userNumLow) * Math.random()) + ' is a number between ' + userNumLow + ' and ' + userNumHigh);

1 Answer

Steven Parker
Steven Parker
231,275 Points

:point_right: Your condensed code is functionally similar to the video example.

You've saved some lines and eliminated some variables by condensing the code, but the functions performed are essentially the same as the example.

The only errors I notice are unrelated to making it more compact:

  • you forgot to add one to the high/low difference to get the correct range
  • after multiplying the range by random, you add the high number instead of the low number
  • you apply parseInt to the random calculation instead of Math.floor (parseInt is for strings)

Thank you, this was very helpful.