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

function name and return value do not have to be the same correct?

I am struggling on question two of placing the new variable and the return function into the code block of the getYear function. How should I structure this?

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

3 Answers

Brian Jensen
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree seal-36
Brian Jensen
Treehouse Staff
function getYear() {
  var year = new Date().getFullYear();
  return year;
}

var yearToday = getYear(); 

Thanks Brian! that second variable was where I got hung up.

** Update. Tried it your way. Says step one of creating the function is wrong now.

Your code is right. First, you have to store the value returned by the "newDate().getFullYear()" function in the variable "year", and then return the value of this variable.

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

Thanks. Unfortunately it will not accept that answer...

Brian Jensen
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree seal-36
Brian Jensen
Treehouse Staff

It is working for me: Screen Shot 2016-12-12 at 14.38.58.png

Did you add the space between new and Date in your original code after adding my variable?

function getYear() {
  var year = new Date().getFullYear(); // on this line
  return year;
}

var yearToday = getYear(); 

It worked for me too... strange !