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 Returning a Value from a Function

I am not understanding the concept of returning the value.

how to complete this code?

script.js
function getYear() {
var year = newDate().getFullYear();
  return getYear;
}
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>

4 Answers

Think of a function as a machine. You put something in, it does some work, you take something out. In code, that will often look like input > calculation > output. If we care about what our function machine is outputting, we can save that result by having the function 'return' a variable or piece of data.

Your function inputs a new Date object, the machine stores the year of that Date into a variable called 'year', and you want to return that output by returning 'year' variable, as shown by Jeff.

Also, to get it to work, you have new Date() as newDate(), it has to be separated:

function getYear() {
var year = new Date().getFullYear();
  return year;
}

hey Dan!! thanks for help but I dont know for what reason they are not letting this pass.

thanks Dan my code finally worked, guess I have problem with the next code.

Jeff Jacobson-Swartfager
Jeff Jacobson-Swartfager
15,419 Points

The value it is asking you to return is year. So, you can just

return year;

within the function's code block to pass this step of the quiz!

I am sorry, but it is not working. can you help?

Jeff Jacobson-Swartfager
Jeff Jacobson-Swartfager
15,419 Points

Absolutely. Return year instead of getYear. So, you can use

function getYear() {
  var year = new Date().getFullYear();
  return year;
}

to pass.

thank you for helping again jeff but the code dont seem to work, I used the exact code.

Thanks jeff, it worked finally, I have a problem with the next code I posted it on my forum, you gotta help me through that!!!