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

I did this same thing with less variables... and I get the same results with less headache....

I feel like he did a few acrobatics that were unnecessary. Sometimes KISS is the best route.

var userInput = prompt('Give me a number and I will try to pointlessly, randomly select another number.');
var userNumber = parseInt(userInput);
var message = "<h2>Your new stupid number is <h2>";
var newNumber = Math.floor( Math.random() * userNumber );
document.write(message + newNumber);

Sorry for poking fun at the exercise. I've actually found his teaching style to be EXCELLENT! It's just my way of coping when I'm tired and still pushing through any sort of work. :)

But I'm serious... this is a much simpler way to get the exact same outputs...and using only the stuff he's taught so far.

1 Answer

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,077 Points

You are technically correct, in the sense that if he was answering the question for the first challenge, he would be overdoing it. However, you missed two important points, one the challenge itself was to generate a random number between 1 and the users number, your script will be between 0 and 1 less than the user's input. Also, he gave a second challenge where the user has to enter two values and the script must then generate a value between the two given numbers. He gave the solution too the second challenge, which is why he has more code than you, as you only answered the first one.

Of course when you get better at javascript and programming in general you will be able to do this much faster and more efficiently.

Hopefully this lets you know why his code was longer than yours....Have A Great Night Learning!!

Oh see... I guess in my fatigue I missed that other part.