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) Creating Reusable Code with Functions Getting Information From a Function

Question an help will be appreciated

I didn't know the purpose of the return value so i tried it out in the work spaces. Then i used this code i will show you the before i put the return value and after i put the return value the code will display below. And if you want to see the results copy those in your work spaces.

Before

function alertRandom() { var randomNumber = Math.floor( Math.random() * 6 ) + 1;

alert(randomNumber) } alertRandom();

After

function alertRandom() { var randomNumber = Math.floor( Math.random() * 6 ) + 1;

return randomNumber; } alert( alertRandom() );

2 Answers

Steven Parker
Steven Parker
231,269 Points

These both work.

I'm not sure what your question is. Could you explain a bit more about what help you need?

I don't know what is the purpose of the return value so i tried it in the work spaces when I tried it in the work spaces I. tried the return value so I see what did it do to the program? and i tried the program before the return value . And i'm asking right now what is the purpose of the return value?.

Steven Parker
Steven Parker
231,269 Points

You did a good job in the second example of returning the value and using for the alert. Your code demonstrates how the return value is passed back from the function for use by the rest of the program. That's really the whole purpose of it.

Your first example does the alert inside, so that function can only be used to do that one job. But your second example could be used for other things, such as instead of an alert, it might be used to pick a photo of a die face and display that., or it might be used in a calculation to determine the result of a game simulation.

Sipann A.
Sipann A.
30,234 Points

I'm not sure what your question is. But in case it is about the purpose of the return value, it "just" means that you ask your function to return the value calculated just before (the randomNumber). If you omit the return, your function will indeed perform the calculation but will keep it for itself while you actually need to get it. If it is not your question, then sorry for not being able to help you.

Thank you sippan, and you don't have to answer my question but thank you for the help.