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

Eli Tamosauskas
Eli Tamosauskas
9,776 Points

Why did he use 2 variables here?

Why does he use 2 different variables for the user number? Why cant you just use one variable like in my code below??

var userChoice = parseInt(prompt("Choose a number?"));

3 Answers

My guess would be that the your code works perfectly fine ( i used the same ) but it is common practise to define it like this. So you will always have your original input to fall back on if needed.

But if someone knows this for sure, i'd love to hear this as well.

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Variables are useful for storing values so it's probably been done just as a way to the question and then have another variable for the answer.

You could maybe do somethong like this

var question = "What is your name?";
var response = prompt(question);

It was probably done to make the code easier to follow?

There is no REAL reason to use two variables - aside from code standards and what not - but why the instructor used two variables is because of his message variable. In order to say "is between 1 and 7", for example, you need two variables to refer to. If you only used one variable, that line would be virtually impossible unless you immediately set the values to another variable AFTER they had been generated. This would cause confusing code for no reason.

Its much easier and cleaner to just have two separate variables.. BUT if you know you won't ever have to refer back to the variables then I don't see why using one would be a problem..