## 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!

### 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.  # return num;

I just want to make sure I'm understanding this correctly. The "num" variable is being saved in the "randomNumber" variable isn't it? I think I'm answering my own question here, but just want to be sure. Thank you!

```var randomNumber = getRandomNumber(10);
var guess;
var guessCount = 0;
var correctGuess = false;
function getRandomNumber( upper ) {
var num = Math.floor(Math.random() * upper) + 1;
return num;
}
```

Thank you Marcus. I understand a lot more now than I did 20 minutes ago. Give this man a raise!!

No problem, David! Happy Coding! Hi Gina,

The 'num' variable is being defined inside the 'getRandomNumber' function as a local variable and isn't available outside of the function. In this instance, it is being used only to help explain the return value.

```var randomNumber = getRandomNumber(10);
var guess;
var guessCount = 0;
var correctGuess = false;
function getRandomNumber( upper ) {
var num = Math.floor(Math.random() * upper) + 1;
return num;
}
```

If you didn't want to do anything else but return the random number, you could chop the function down to be:

```function getRandomNumber( upper ) {
return Math.floor(Math.random() * upper) + 1;
}
``` Thanks Damien, Here's all of the code...I'm just trying to understand how the returned "num" value gets assigned to the "randomNumber" variable. (and why can't I get the code to show like yours is showing? I added the three ticks and "javascript"...sorry about that!

```var randomNumber = getRandomNumber(10);
var guess;
var guessCount = 0;
var correctGuess = false;
function getRandomNumber( upper ) {
var num = Math.floor(Math.random() * upper) + 1;
console.log(num);
return num;
}
do {
guess = prompt('I am thinking of a number between 1 and 10. What is it?');
guessCount++;
if (parseInt(guess) === randomNumber) {
correctGuess = true;

}
} while (!correctGuess)
document.write('You guessed the number ' + randomNumber + ' in ' + guessCount + ' attempts.');
console.log(randomNumber);
```

Hey Gina,

Use this photo as a guideline for how to format your code. The 3 backticks should be on their own separate lines. Also, ensure there are blank spaces above the first 3 backticks and below the last 3 backticks.  'getRandomNumber' is a function which can receive a variable, process it and return a result. In the example below, 'randomNumber' is set to the result 'return'ing from 'getRandomNumber'. The function returns a value, the 'num' is just how that value is tracked inside the function.

You can call a function what ever you like, which helps if it describes what the function does. Breaking it down, this is what is happening:

```// Pass '10' into 'getRandomNumber', this returns a value which is stored in 'randomNumber'
var randomNumber = getRandomNumber(10);

function getRandomNumber( upper ) {
var num = Math.floor(Math.random() * upper) + 1;
return num;
}

// If you were to remove the 'function' stuff out of the above, it would look like:
var randomNumber = Math.floor(Math.random() * 10) + 1;
```

Does this help explain it?

No problem! :)

Follow up questions: Why do we need to first assign the function to a variable and then return a variable in order to return a value?

```function getRandomNumber( upper ) {
var num = Math.floor(Math.random() * upper) + 1;
return num;
```

Is there a way to do both in one step? Such as...

```function getRandomNumber( upper ) {
return num = Math.floor(Math.random() * upper) + 1;
```

Hey David,

You definitely don't have to set num at all here. You could just return everything in the return statement:

```function getRandomNumber (upper) {
return Math.floor(Math.random() * upper) + 1;
}
```

Thank you Marcus. Is the advantage that you can use the return value as a variable more easily? If not, can you help me to understand why we would return a value as a variable?

There's no reason to set a variable here because `num` is destroyed as soon as the function completes its execution. You couldn't use `num` outside of this function (as is) since it is created as a local variable and remember that JavaScript operates on a function scope where function variables are local variables and all other variables are global.

If, however, you left the "var" keyword off of `num`, it will be hoisted into the global scope and available to other pieces of code, including functions. However, this is not advised unless you specifically need that variable to be available in other functions; in this case, you do not.