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

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 !

function returnValue (getRandomNumber) {
  return getRandomNumber  ; 
function returnValue (leo) {
 var echo =  returnValue (leo) ;
  return leo ;
returnValue ( " skata " ) ;
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>JavaScript Basics</title>
<script src="script.js"></script>

2 Answers

Jennifer Nordell
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:

1,324 Points

oh .. finally !!

Yes now it seems prety clear ..

Thank you Jennifer !!