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

Jason Hess
Jason Hess
4,789 Points

Storing the returned value of the function in a new variable named yearToday

Step 1: Create a function named getYear

function getYear () {

}

Step Two: add this variable to the functions code block, var year = new Date().getFullYear();

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

Step 3: This is where I am getting stuck... "Call the getYear function: store the returned value of the function in a new variable named yearToday.

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

or

should my code just simply be:

function getYear () { 
    var getYear = yearToday;
};

Do I even need the "var year = new Date().getFullyear();" code block that was asked of me in step 2? I don't know why but this JS is getting the best of me:-( Thanks in advance! Sorry if my question is in disarray with the syntax, just getting frustrated with JS at 1:10 am EST and I have to wake up in 4 hours. BLAH!!

script.js

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>

2 Answers

Hi Jason,

I think you are overthinking this one!

Simply ask yourself what is it that the function is returning. Try it out in a console (even your browser's, or a website like JSfiddle). Ultimately, the function is returning the full year of a newly created Date object (which will be our current year). You can read more about Date here.

This is the information that the function has returned, but the next step is asking you to store the information called with the getYear() function in a new variable, yearToday. You can achieve this like so:

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

var yearToday = getYear();

Happy coding! Leslie

Thomas Nilsen
Thomas Nilsen
14,957 Points
function getYear () {
   var year = new Date().getFullYear();
   return year;
};

Your function also need to return the value