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

John Gilmer
PLUS
John Gilmer
Courses Plus Student 3,782 Points

Not sure what im getting wrong lol

Not sure what it wrong, I think it's a problem returning the function, must of forgot how to return a function

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

return function year();
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

The first step is to create the empty function:

function getYear(){}

Step two add the line var year = new Date().getFullYear(); inside the function and return the variable year.

function getYear(){
  var year = new Date().getFullYear();  //this adds the requested line
return year; //this returns the year variable
}

Step three call the getYear function and store the returned value in a variable called yearToday.

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

var yearToday = getYear(); //this creates a variable named yearToday and sets it equal to the value returned from calling (also known as invoking) the function getYear. You call (or invoke) a function in javascript by putting the function name followed by an open in closed parenthese () for example: getYear()

It wants you to put the return in the function so that when you call the function it will actually return the year. Without the return in the function it wouldn't really do anything other than store the variable year.

function getYear() {
    //store a variable that will get the year
    var year = newDate().getFullYear();
    //return that variable when the function is called
    return year
}