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 Passing an Argument to a Function

Josh Wozniacka-Knoll
Josh Wozniacka-Knoll
8,796 Points

My brain is not working properly, Can you please help??

I can't seem to figure this one out.

script.js
function returnValue ( drink ) {
  var echo = drink;
  return echo + " ";
}
returnValue(echo, "hot");
index.html
<!DOCTYPE HTML>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>JavaScript Basics</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
Josh Wozniacka-Knoll
Josh Wozniacka-Knoll
8,796 Points

After your newly created returnValue function, create a new variable named echo. Set the value of echo to be the results from calling the returnValue function. When you call the returnValue function, make sure to pass in any string you'd like for the parameter.

I can't figure out how to do this part...

4 Answers

andren
andren
28,558 Points

Your brain is working just fine. Returning values and assigning the result is something that confuses a lot of people when they are just starting out programming.

When you use the return keyword that results in the value you return basically being sent back to the place in the code where you called the function. If you want to assign that value to a variable you simply use the = operator like you would when assigning it any other value.

Also the task asked you to create the variable below your function, you have created it within your function. If you move it below your function and then make it equal to calling the function with a string as a parameter like this:

function returnValue(drink) { // Take whatever is passed in and assign it to the parameter "drink"
  return drink; // Return the contents of the parameter "drink"
}
var echo = returnValue("hot"); // Set "echo" equal to whatever the "returnValue" function returned.
// After running this code "echo" will equal "hot" since that is what you passed in to the function.

Then your code will work. I also added some comments to clarify the code a bit.

Josh Wozniacka-Knoll
Josh Wozniacka-Knoll
8,796 Points

Thank you so much that was throwing me for a loop.

Lol this question freaked me out :laughing:

Danny Rubio
Danny Rubio
2,546 Points

I had the exact same issue, but I was understanding that they wanted the variable inside the function but I'm glad i was able to find this. Thanks for the help Andren and thanks for asking the question Josh