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

Jason Williams
Jason Williams
6,691 Points

Could I get some feedback please to see if this is correct.

var numberCollected = prompt("write a number here between 1 - 100");
var randomNumber = parseInt (Math.random() * (numberCollected - 1)) + 1;
document.write(randomNumber);

4 Answers

David Morisset
David Morisset
13,323 Points

The code looks nice and it works when I try it in the console, so I'd say it's very good.

Jason Williams
Jason Williams
6,691 Points

Thank you for your feedback

Steven Parker
Steven Parker
231,275 Points

Why ask only for numbers between 1 and 100? There's no enforced limit and the random generator is capable of picking a number within any specified range (isn't that the point?)

But good solid generator code. :+1:

I think you're doing it wrong. The variable numberCollected is still a string when -1 and Math.random are done. I think it should be:

var numberCollected = prompt("write a number here between 1 - 100");

var randomNumber = Math.random() * (parseInt(numberCollected) - 1) + 1;

document.write(randomNumber);

What is the -1 in the code suposed to do?

Jason Williams
Jason Williams
6,691 Points

Thanks for your kind feedback. Yes I appreciate your comment and have come a long way since this early question and understand the concept of this and returning a number not a string. However thanks again