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

Paul Carrillo
Paul Carrillo
8,100 Points

Storing the returned value

I keep getting an error with my code. I am placing the returnValue in
my variable echo. But its keeps giving me an error. If I delete line two so it can return echo its saying Task 1 is not complete.

script.js
function returnValue(value) {
  return value ;
  var echo = value + " is good for you";
  return echo;
}

returnValue('pizza');
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>

This is because you are returning something 2 times. 'return' can ONLY be used once in a function, at the very end. Try just returning echo.

Hope this helped!

1 Answer

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

Hi there, Paul! I think you're doing fairly well but I think you missed part of the instructions. The instructions explicitly say to make the variable echo after your newly created function. But you're making it inside the function. Also the value of echo should contain the result of sending the string into the function. Here's your code reworked a bit:

function returnValue(value) {
  return value;
}

var echo = returnValue('pizza');

We first declare a function that takes a value. We then immediately return a value. It's important to note here that once a return statement is hit in a function, the function exits. This means that the code you wrote would never get to the second return statement.

Now we declare our echo value and set it equal to the result of calling that function and passing in "pizza". The function returns "pizza" and assigns it to echo. If you were to now use console.log("I like " + echo); You'd receive "I like pizza". Hope this helps! :sparkles: