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

Sean Flanagan
Sean Flanagan
33,235 Points

Task 2

Hi.

Can anyone help me please? I don't know what I'm supposed to do.

Thanks

Sean

script.js
function returnValue(str) {
  return str;
}

function returnValue(hello) {
  var echo = width * length;
}
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>
Sean Flanagan
Sean Flanagan
33,235 Points

I don't know what to put after var = echo.

2 Answers

Matt F.
Matt F.
9,518 Points

Hello there.

They want you to define a function (first three rows). Then call the function (last row to the right of the =) and then store the result of the function call into a variable named echo (last line to the left of the =).

function returnValue(yourValue){
  return yourValue;
}

var echo = returnValue("This is the string literal");

On a side note --it is often considered better practice to define the variable at the top of the scope, like this:

var echo;

function returnValue(yourValue){
  return yourValue;
}

echo = returnValue("This is the string literal");

I put the variable definition on one line in the first example for brevity and clarity.

The equals sign is used to assign a value to a variable. e.g.:

var myVariable = "value";

The value can be just about anything (string, number, array, object, function, etc.). In this case, they want you to call the function and assign the return value to the variable.

In other words, the variable value will equal the result of running the function.

You call a function using the function's name followed immediately by a set of parentheses. e.g.:

myFunction()

You can also pass in arguments or parameters into the function so that it can use them only within that function. e.g:

myFunction("one", 2, [3, 3, 3])

A string is a value enclosed in quotes (either single or double, just make sure to use the same to close/finish the string).

So combining all that, you should be able to pass a string into the function you created and assign that to the variable echo.

Good luck and let us know how you go!

Sean Flanagan
Sean Flanagan
33,235 Points

Hi Iain and Matt.

Sorry for the delay but it was the middle of the night where I live and I was very tired!

Just to let you know I used Matt's syntax for Task 2 and it worked a treat. It's done.

Thank you both

Sean