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

Joe Worthington
Joe Worthington
1,329 Points

Getting used to sharing my code. My random number generator.

Javascript
//Program start
console.log("Start");

//User instruction and input:
var userNumber = prompt("This is a random number generator. Please insert a number of your choice and a random number (between 1 and your chosen number) will be returned to you. Free of charge! Click okay to continue");

//String to integer
userNumber = parseInt(userNumber);

//User confirmation 
alert("You've chosen " + userNumber + ". Click okay to claim your reward!"); 

//Creates the random number
randomNumber = Math.floor(Math.random() * userNumber) + 1;

//Writes the random number to the document
document.write("You\'re random number is " + randomNumber);

//Program ends
console.log("end");

2 Answers

Steven Parker
Steven Parker
230,248 Points

Works just fine! :+1: But I have a few (all minor) comments:

  • It seemed odd to me that the communication through prompt/alert ended on the document. I expected to see the result in an alert as well.
  • The alert after the prompt could be skipped, and the number could be given after the prompt has been satisfied.
  • You could add validity checking on the input, so a non-number would cause an error message and require re-entry instead of delivering NaN as a result.
  • "You're" is a contraction of "you are". The possessive form of "you" is simply "your".
Joe Worthington
Joe Worthington
1,329 Points

Hi Steven, thanks for taking the time to read my code and give me feedback.

With regard to your expectations of the result, I agree this sort of continuity is important, yet the task was to write the answer to the document. I suppose these considerations are something you learn with experience.

The validity check is a great idea, thanks. I'll give that a go.

And, damn my grammar!

Cheers