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

Hi, where is found the error?

where is found the error?

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

returnValue('hello')
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>
Erik Nuber
Erik Nuber
20,629 Points

This isn't actually doing anything. If you run it in the console log you can check for the value of returnValue and echo but, other than that nothing is going on.

if you want to fix this to actually show hello you can create a div in your html, give it an id and then use

document.getElementById("yourIdName").innerhtml = returnValue('hello'); 

within your javaScript

4 Answers

Erik Nuber
Erik Nuber
20,629 Points
var echo = returnValue("hi");

that goes outside the function.

My apologies on my first answer, didn't realize it was a challenge.

Tom Byers
Tom Byers
13,005 Points

Hi Giovanni,

The question asks you to create the variable after the returnValue function. I think you're over-complicating it :). See if structuring your code like this helps:

function returnValue(h) {
  return h;
}

var echo = returnValue('hello');
Joel Bardsley
Joel Bardsley
31,249 Points

Hi Giovanni, your code returns 'hello' with no problems - the issue with this challenge is it's asking you to immediately return the argument passed into the function. With your provided code, you could just return h; without the need to create a new variable to assign the value of 'h' to.

Hi Joel! thanks for the answer. My problem is in the task 2:

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

Error is "Hmmm. It doesn't look like you're storing the returned value in the echo variable."

Joel Bardsley
Joel Bardsley
31,249 Points

Sorry about that I should've gone through the complete challenge before posting an answer! As for the second part, the key word is after the returnValue function, whereas your echo variable is inside your returnValue function.

You can set the value of the function as a variable by doing var echo = returnValue("anything");

Thank you very much for your answers!!