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

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

Bryan Knight
Bryan Knight
34,214 Points

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
}