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

Scott Rushford
Scott Rushford
1,724 Points

I am getting an error when submitting due to the argument not being stored into the echo variable properly. Any ideas?

Here is the code I am working through in the Passing an Argument to a Function section. function returnValue(testString){ var echo = testString; return echo; } returnValue("test");

I don't think I am missing anything, but I am open to suggestions and clarification.

script.js
function returnValue(testString){
  var echo = testString;
  return echo;
}

returnValue("test");
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

You want to simply return the given value to your function without assigning it to anything. The second challenge is wanting you to assign the value returned by your function to the echo variable.

function returnValue(variableToReturn) {
    return variableToReturn
}

var echo = resturnValue('whatever is passed in here will now be assigned to the echo variable')
Scott Rushford
Scott Rushford
1,724 Points

Right on the button Joe... I wasn't really thinking that I could assign it directly to "echo" right off the get go. It also makes sense because I need to bring it out of the function to use it in the scope of the entire program. Appreciate the help!!

Hi,

The code JS code should look similar to that below:

function returnValue(testString) {
 return testString;
}

This will simply accept an argument and return it.

Scott Rushford
Scott Rushford
1,724 Points

Thanks for the help Martin! Seems pretty straight forward, but I was in a little fog for structure!! Appreciate the assist!