Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics (Retired) Creating Reusable Code with Functions Passing an Argument to a Function

Todd Teetzel
Todd Teetzel
789 Points

Stuck on calling a function and assigning a variable

"Here's a simple challenge: create a function named returnValue() that accepts a single argument, then returns that argument. This isn't a useful function, but we want to make sure you know how to create a function that can accept information when it is called."

function returnValue(string){ return(string) }

I'm confused about my next step. Please advise. Thank you!

script.js
function returnValue(string){
 return(string)
}
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>

3 Answers

The next step is to actually call the function, to do this, you simply state the function name with any parameters in parenthesis. Try this:

var echo = returnValue('this is how you call a function')

Million Asseghegn
Million Asseghegn
3,845 Points

but you need to write this code after you close the bracelet.

Erik McClintock
Erik McClintock
45,783 Points

Todd,

I'm not sure whether you're saying you're having difficulty with part 1 or 2 of this task, so here is some help for both portions:

Part 1)

You have some syntax issues here that you could fix. Take a look below.

This is what you have:

function returnValue(string){
 return(string)
}

This is what you want:

function returnValue(string){
 return string;
}

Note the removal of parenthesis at your return value, and the addition of a semi-colon to end the statement.

Part 2)

In part two of the task, they are asking you to pass in an actual string as the value for the argument of your function, such as the string 'my argument'. This could be any string value that you want, though, like 'My name is Todd'.

returnValue('my name is todd');

The above code calls the function that you previously defined, called "returnValue", and applies the string 'my name is todd' to your argument (which you defined as "string" when you created the function). Since the function simply returns whatever value is passed into it as your "string" argument, and we passed in the string 'my name is todd', this will simply return 'my name is todd' when we call it.

Hope this helps to make things a little clearer.

Erik

Todd Teetzel
Todd Teetzel
789 Points

Awesome, thank you Erik and Michael. I finally got it. Much appreciated guys!