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

What am I doing wrong in this JavaScript code?

I get this message: Bummer! Hmmm. It doesn't look like you're storing the returned value in the echo variable.

script.js
function returnValue( fruit ) {
  var echo = fruit;
  return echo;
}

returnValue("apple");
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>

3 Answers

Bill Hinostroza
Bill Hinostroza
19,273 Points

This is my code and It passes.

var echo;

function returnValue(str){
   echo = str;
   return echo
}

returnValue("Bill")

Okay, I had that at first, but was declaring the variable in the wrong place. Thanks for your help.

Steven Parker
Steven Parker
221,450 Points

You were really close!

Task 2 says: "After your newly created returnValue function, create a new variable named echo. Set the value of echo to be the results from calling the returnValue function. When you call the returnValue function, make sure to pass in any string you'd like for the parameter. "

:point_right: Notice that it says to create the variable after the function, not inside it.

Just put the function back to the way you had it in task 1, and then crreate and assign the new variable when you call your function.

I tried doing it outside of the function and get the same message.

Tobias Helmrich
Tobias Helmrich
31,602 Points

Hey there,

Steven actually already gave you the right solution. I just want to add the code so you can see where your problem might be. Bill's code may pass but he's creating the variable outside of the function and is accessing it in the function outside of its scope which probably isn't the best solution for this challenge.

function returnValue(fruit) {
  return fruit;
}

var echo = returnValue("apple");

It should work like this, it's just the code for the solution Steven gave you, I hope that helps! :)