Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics (Retired) Creating Reusable Code with Functions Returning a Value from a Function

What is going on ?????

Oops! It looks like Task 1 is no longer passing.

Get Help Recheck work Go to task One




1 function getYear() { 2 var year = new Date().getFullYear(); 3 return year; 4 } 5 getYear(); 6 var yearToday = year;

function getYear() {
    var year = new Date().getFullYear();
    return year;
var yearToday = year;
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>JavaScript Basics</title>
<script src="script.js"></script>

1 Answer

Jennifer Nordell
Jennifer Nordell
Treehouse Teacher

Hi, Martin! You're doing splendidly! But I have a feeling that you haven't really gotten into the idea of scope yet. That variable year only exists inside that function getYear. Once that function ceases to execute, the variable year goes "out of scope" or disappears. You no longer have access to it. This is why we have the return statement.

The reason it's saying that Task 1 is no longer passing is because there's an error in your code because of the year being undefined outside of the function and JavaScript doesn't understand to what you're referring.

Here's the line you're missing:

var yearToday = getYear();

This declares a new variable named yearToday. It will call/execute/invoke the function getYear. When we use the return statement we are returning the value to the place where it was called. In this case, the year will be returned to the right side of the equals sign and assigned back into the variable yearToday.

Hope this helps! :sparkles: