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

Joshua Phillips
Joshua Phillips
3,429 Points

function test

I am having trouble understanding what I should do to make the variable work with the function and the return. I have been trying to find the answer out myself, but I know I am missing something so simple for the solution. Please help!

script.js
function returnValue (car) {
  var echo = returnValue (car);
         return car;
}
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>

2 Answers

Joel Bardsley
Joel Bardsley
31,249 Points

A couple of things wrong there:

Firstly you're trying to call your function within the function - take out the var echo line and move it outside of the function. Lastly, when calling the function you're passing an undefined car as the parameter/argument. You can either create a variable called car and give it a string value, ie:

var car = "Honda";
var echo = returnValue(car);

or you can pass the string into the function directly:

var echo = returnValue("Honda");

Hope that clears things up for you.

Joshua Phillips
Joshua Phillips
3,429 Points

Thank you! Moving the variable outside the function was what I needed.