Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
221,295 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