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

Cesare Parmiggiani
Cesare Parmiggiani
8,017 Points

Task 2 of 2

Can someone explain me what i have to do in this exercise?

I don't understand task 2 of 2

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

Hi Cesare.

You are looking at the problem from a false perspective:

var returnValue = function(argument){ //here you declare a function named returnValue -> what you write in the "()" is this so called argument that you pass to the function - you declare those so that the function knows its expecting some variable - argument passed to it so you can access it's value in the function or this value won't exist -> because of "Variable scope"
    return argument;  // with the keyword return you specify that when the function ends (it does its magic before its done) it should return the value you want back to the upper level scope - in short "return" the value of "argument"
}

var echo = returnValue('My argument'); //here you declare a variable ("echo") that will get the value of the declared function ("returnValue") from the return statement "return argument" -> the variable "echo" will have the value of "argument"

I hope this helped. If not don't be afraid to ask again.

You can either add me to skype: nejc.vukovic@gmail.com or check my Twitter feed @n_WEB_d and ask there.

Oh I almost forgot: happy coding and enjoy your evening.

Cesare Parmiggiani
Cesare Parmiggiani
8,017 Points

Thank You Nejc

I'm starting approach Javascript, and i'm a little confused by functions.

Thank you

Cesare

Hey Cesare no worries.

Starting is the thing that matters. With practice you'll get the hang of it.

You'll see: sooner or later -> you and functions will become best buddies :)

Hi Cesare,

I think you were over thinking it. First Create the function named returnValue "function returnValue"

that accepts one argument "(arg)" and returns that argument "{return arg;}"

function returnValue (arg) {
return arg;
}

I always break it down to the individual parts and then put those parts together. Makes it easier for me. Let me know if you still have questions.

Step 2

Ok to call the function (remember to call a function you use the function name plus "();") so you would do "returnValue();" pass in a literal string value "returnValue('This is my string');" store (aka assign) the results of the function call to a variable named echo. So first you can create the variable echo "var echo" and assign it (use the "=" to assign / store a value in a variable) to the results of calling the function.

var echo = returnValue('This is my string');

from the way the function is structured in task one, it's only job is the return whatever argument you passed into it. So at the end, echo would contain 'This is my string' , because returnValue function would spit that string out as it's result. Does that make sense?

Cesare Parmiggiani
Cesare Parmiggiani
8,017 Points

Thank you Brian!

But the problem is the second part of the question:

"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."

I don't understand what I have to do....