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'm typing exactly what it tells me to and it still isn't working.

When it tells me to use "return" it then says Task 1 is no longer running.

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

Hi Ellen,

You typed it correct. But the challenge also asks you to return the variable. So you should add a return statement in your function.

Like this:

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

HI Ellen, Looks great, just Add return year; Before last curly brace

Emil Rais
Emil Rais
26,873 Points

Have you been returning the value directly rather than the variable? I.e:

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

You should first store the value in the year variable and then return the year variable. Then the parser that checks your code is satisfied.

Under normal circumstances I would always return the value directly rather than first storing it in a variable. The only exception is when the code that has produced the value is so complicated that the variable will serve as useful documentation. This is not even the case here as the value is clearly described by the code producing it as well as the function name it serves as a result for.