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

LEONIDAS MANOUSAKIS
LEONIDAS MANOUSAKIS
1,324 Points

Solution in Javascrip Basic

Can someone help me with the solution on this one cause i m rly stuck and pissed off !

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

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! First, you're doing better than you give yourself credit for and when you see the solution, it'll probably feel obvious. Your first function was spot on! So well done.

But then you made a copy of the first function, which contained a variable that was calling the function itself with the thing that was passed into it already. It's here I feel like you overthought it.

Here's my solution and I'll walk you through it:

function returnValue (getRandomNumber) {
  return getRandomNumber  ; 
}

var echo = returnValue("Hi there, Leonidas!");

It's the last line where you got tripped up. Here we make a variable named echo. I know you know how to make variables :smiley: Now once we do that, we're going to call the function. When we say "call the function" we mean we are telling it to execute. The thing we're sending in is called the argument. In this case, I'm sending in "Hi there, Leonidas!". The function is going to take that argument and assign it to a local variable (only visible to that function) and assign it to a variable getRandomNumber. At this point, getRandomNumber has been assigned the value "Hi there, Leonidas". Then we're going to immediately return the value of the variable to the piece of code that called it. Now this is a really simple example, but imagine for a moment that you had to send in a number and do some fancy math calculations on it. We'd probably want the results to come back to us so that we can use them for something else. This is what the return statement is for. So "Hi there, Leonidas!" is returned to us at that spot. Because it's on the right side of the equals it is assigned into the variable echo. At this point, the value of echo is "Hi there, Leonidas!".

Hope this clarifies things! :sparkles:

LEONIDAS MANOUSAKIS
LEONIDAS MANOUSAKIS
1,324 Points

oh .. finally !!

Yes now it seems prety clear ..

Thank you Jennifer !!