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

I think I have a syntax error, but I can't figure this one out....Can anyone offer my some insight?

It says my variable 'echo' is not receiving the value of the function 'returnValue()'... I have it typed var echo = returnValue();

I've been able to figure out all the previous challenges, but this one has me stumped....

script.js
function returnValue(story) {
  return story;
}
var echo = returnValue();
returnValue('name');
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>
Simon Coates
Simon Coates
28,694 Points

unless this is a casual error, you may have some flaws in your understanding of the concept. The function there returns the result of its execution at a particular moment. when you assign echo, it doesn't have a value to use. When you give it the right values, you aren't using the returned value. (If this doesn't make sense, can you explain your understanding of your code?)

Aaron Martone
Aaron Martone
3,290 Points

What's happening in your script.js is this: You create a function (returnValue) that takes an argument (story) and simply returns it back to the caller. Then you create a variable (echo) and assign it the value of the returnValue method's return value (the parens () at the end of that line actually invoke the method, and since they did not provide a value, undefined was given as a value of story, so it returned undefined, thus, echo now holds the value undefined. Lastly, your execute returnValue(), sending it an argument value of name, which is returned by the function, but never assigned into any variable or output to the console, so the returned name is lost and never assigned.

Aaron Martone
Aaron Martone
3,290 Points

Oh, and sorry if I'm being semantic, but you'll always know when there's a SyntaxError. Such an error means that the code you write did not follow JavaScripts rule's, ie its syntax (what you wrote), cannot be understood. Your code is syntactically sound (ie, it's pure JavaScript). You're just not getting the expected result. These types of errors are always much harder to troubleshoot. This is a Logical error. Where you're not getting what you expect, and have to follow the code execution procedurally, dump values to console and ensure that things are following as you intended to.

Keep at it. It DOES get easier in time. Once you learn the actual HOW of JavaScript's execution, things will start to make more sense.

2 Answers

Simon Coates
Simon Coates
28,694 Points

try

function returnValue(story) {
  return story;
}
var echo = returnValue('name');

Simon & Aaron, Thank you both for your help. After you pointed out it was a logic error, and I looked at it again with fresh eyes.... it makes sense. When I'm explained the mechanics, I can usually understand it....but I'm not far enough in yet, to where I can always think of it myself. I'm getting close to a deadline for the pre-work I need to finish to enroll in my towns 'code challenge'....and I was starting to freakOut.....so thanks for helping me get unstuck!

Aaron Martone
Aaron Martone
3,290 Points

No problem Isaac. You and I are alike. We don't like just being given the answer without understanding why. If we do not comprehend the process to how the result came, we have learned nothing. That's why I was hoping that a step-by-step processing of your code (as if I were the JS interpreter) might prove useful why it wasn't running as expected. Glad to hear it helped.