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

Michael Stambulyan
Michael Stambulyan
3,220 Points

Stuck ! cant figure this one out, please help.

how do I add a Variable to this function ?

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

4 Answers

Michael Stambulyan
Michael Stambulyan
3,220 Points

thanks for the help :) the task im trying to do is ...... "Now that you've created the returnValue function, call it, by passing it a literal string value -- a series of characters in quote marks like this: 'My argument'. Store the results of the function in a variable named echo."

You had the right idea, but dont wrap ur function name in quotes, just wrap the parameter in quotes, and im assuming it wants u to call it outside of the function, otherwise u would have recursion going on. so try this

function returnValue(hello) {
  return hello;
}

var echo = returnValue('hello');
  1. You are missing semicolons

  2. Not sure what your trying to do here var echo = "returnValue(hello)" but this will assign a string "returnVale(hello)" to the variable echo and then you never even use your variable

  3. You return hello, which basically means your functions does nothing but spit back out what you passed it

Well, for one, you're forgetting to put the semi colon ; after you assign the variable and it looks like you're trying to interpolate in a string and call the function on its self, both of which will not work. I think you were meaning to use console.log();.

You can define variables in these ways.

function returnValue(hello) {
  var echo = console.log(hello);
  var warn = console.warn(hello);
  var error = console.error(hello);
}

or you can define the variables like this:

function returnValue(hello) {
  var echo = console.log(hello), // separate variable definitions with a comma
        warn = console.warn(hello),
        error = console.error(hello); // Only the last defined variable needs to be closed with semicolon
}

Hope this helps you.

Michael Stambulyan
Michael Stambulyan
3,220 Points

thank you ! that was it, just had to write it outside of the function.

no problem :)